Click here to Skip to main content
15,896,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i'm taking a list to bind in view so i have the details when page loads in the same view i wanna perform edit on when i click on edit button


this is the action which i use to show the details when page loads
public ActionResult EditEmployee(int id = 0)
{
return View(db.Employees.Where(o => o.EmployeeID == id).ToList());
}

And this the view I'm using and wanna perform edit on update (submit)buttton



@foreach (var item in Model)
{

FirstName @Html.EditorFor(model => item.FirstName)
LastName @Html.EditorFor(model => item.LastName)
Title @Html.EditorFor(model => item.Title)
BirthDate@Html.EditorFor(model => item.BirthDate)
Address@Html.EditorFor(model => item.Address)
City@Html.EditorFor(model => item.City)
<input type="submit" value="Update" id="update" />

<script type="text/javascript">
$(document).ready(function () {
$(".datepicker").datepicker();
});






})
</script>
}



after this how can i call the action to perform the edit
Posted
Comments
TryAndSucceed 2-Oct-13 10:52am    
Try Web Routing. Use two Action Methods [get] and [post] for Edit, so you go to POST method of Edit when you submit the form OR have another method and redirect to it from your button.

1 solution

Your edit action can receive the form fields as "FormCollection" and access them as follows:

C#
[HttpPost]
       public ActionResult EditEmployee(FormCollection order)
       {
           string FirstName = Convert.ToString(order["FirstName"]); //access your form items like this
}


The best way is to have strongly typed model such that you have have your code as follows:

C#
[HttpPost]
       public ActionResult EditEmployee(Order order)
       {
           string FirstName = Order.FirstName; //access your form items like this
}


In order to have your strongly typed object you have to change your edit action as follows:
C#
public ActionResult EditEmployee(int id = 0)
{
return View(new List<order>(db.Employees.Where(o => o.EmployeeID == id).ToList()));
}
</order>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900