ASP.NET MVC editing fast a property

There are some moments when you want to fast edit a property ( like a status or a name) and you do not want to load the entire “Edit” form for this.More,you are in an edit formula and do not want to add a form.

So here is the solution in ASP.NET MVC  with jquery-1.4.2.min,jquery-ui-1.8.1.custom.min for a modal dialog and some controllers. The most difficult were the javascript,so I let to the final.

We will be editing the name for an employee in 4 steps.The most difficult to understand is the step 4(javascript),however,it can be re-used with any other object- so you can put it in a common js file.

Step 1

create a view for editing the name,named FastEditEmp


<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<BusinessLogic.Employee>" %>
<!-- edit inline the name -->
<%= Html.HiddenFor(Model=>Model.ID) %>
<%= Html.LabelFor(model=>model.Name) %>
<%= Html.EditorFor(model=>model.Name) %>

Step 2

Create the controllers and verify it works – at least the get  action .


public ActionResult FastEditEmp(int id)
 {
 var emp=EmployeeList.LoadFromID(id);
 return View(emp);
 }
 [HttpPost]
 public ActionResult FastEditEmp(BusinessLogic.Employee emp)
 {
 try
 {

 var dbid = EmployeeList.LoadFromID(emp.ID);
 UpdateModel(dbid);
 dbid.Save();
 return new JsonResult() { Data = new { ok = true,message = "succes" } };

 }
 catch (Exception ex)
 {
 //TODO : log
 return new JsonResult() { Data = new { ok = false,message = "error : " + ex.Message } };

 }
 }

Step 3
Create the hyperlink to edit the name of an employee (emp) :

<a href=’javascript:EditModal(<%= emp.ID%>,”<%=Url.Action(“FastEditEmp”,new {id=emp.ID}) %>”)’>Edit name</a>

Step 4

This is the most difficult one,the javascript. However,more of the code is common no matter what you want to edit fast modal :


var wait = '&lt;%= ResolveUrl(&quot;~/Content/Images/wait.gif&quot;) %&gt;';
function EditModal(empID,url)
{
// load a please wait dialog
 var $dialog1 = $('&lt;div&gt;&lt;/div&gt;')
 .html(&quot;Loading data &lt;br /&gt; &lt;img src='&quot; + wait + &quot;'&gt;&lt;/img&gt;&quot;)
 .dialog({ autoOpen: false,
 title: 'please wait'
 });
 $dialog1.dialog('open');
// so the please wait dialog was shown to the user. Now load the content of the action specified in url
 $.get(url,
 {
 ID: empID
 },
 function(txt) {

 var $dialog = $('#dialog');
 $dialog.html('');
 $dialog.html(txt)
 .dialog({
 autoOpen: false,
 title: 'Edit',
 modal: true,
 show: 'blind',
 hide: 'explode',
 closeOnEscape: false,
 buttons: {

 &quot;Close&quot;: function() {
//just cleanup - no saving
 var allInputs = $(&quot;:input:not(:button)&quot;);
 var ser = allInputs.serialize();
 allInputs.remove();
 allInputs.empty();
 $(this).dialog(&quot;close&quot;);
 $(this).dialog('destroy');

 },
 &quot;Save&quot;: function() {
//now saving data : serializing and posting
 var allInputs = $(&quot;:input:not(:button)&quot;);
 var ser = allInputs.serialize();
 allInputs.remove();
 allInputs.empty();

 window.alert(ser); -- debug mode
 $(this).dialog('close');
 $(this).dialog('destroy');
//saving data by posting to same url!
 $.post(url,
 ser,
 function(text) {
 $(this).dialog(&quot;close&quot;);
 $(this).dialog('destroy');
 if (text.ok) {

 window.alert(&quot;Saved - you can change here the display id with jquery&quot;);
 window.location.reload(true);
 }
 else {
 window.alert(text.message);
 }

 }
 );

 }

 }

 });

 $dialog1.dialog('close');//closing the wait dialog
 $dialog.dialog('open'); // show main editing dialog
 });

}

 &lt;/script&gt;

As always,please find here the saving modal files. Please execute first the emp.sql file,then modify the connection string into the web.config .


Posted

in

by

Comments

7 responses to “ASP.NET MVC editing fast a property”

  1. […] This post was mentioned on Twitter by ignatandrei, Larry King. Larry King said: ASP.NET MVC editing fast a property « A Programmer with Microsoft … http://bit.ly/aTGSKm #MVC […]

  2. vks Avatar
    vks

    could u please define as i am new to ajax and jquery that, what following line means here :
    var allInputs = $(“:input:not(:button)”);
    33 var ser = allInputs.serialize();
    34 allInputs.remove();
    35 allInputs.empty();
    36 $(this).dialog(“close”);

    1. Andrei Ignat Avatar
      Andrei Ignat

      line 1 : get all inputs that are not buttons
      line 33 : serialize into a string
      line 34/35: remove from DOM (GUI, if you prefer)
      line 36 : close the dialog with the form

  3. Nhut Huynh Avatar
    Nhut Huynh

    Hi Andrei Ignat,
    your posts are very helpful. but i have question for this post, i wonder if how do i edit an image or file that i uploaded to server, and after serialize all input value into a string, whether how to specific which value fields for right properties in entity ? And finally when update successfully how to update or refresh to load new information on table list ? thanks in advance

  4. […] that case i need redirect to previous page automatically too, i was looking for it and only found this that break the first concept because is dependant of javascript. Both ways are incompatibles and […]

Leave a Reply

Your email address will not be published. Required fields are marked *