0

I am calling Edit action from my view that should accept an object as parameter.

Action:

[HttpPost] public ActionResult Edit(Organization obj) { //remove the lock since it is not required for inserts if (ModelState.IsValid) { OrganizationRepo.Update(obj); UnitOfWork.Save(); LockSvc.Unlock(obj); return RedirectToAction("List"); } else { return View(); } } 

From View:

 @foreach (var item in Model) { cap = item.GetValForProp<string>("Caption"); nameinuse = item.GetValForProp<string>("NameInUse"); desc = item.GetValForProp<string>("Description"); <tr> <td class="txt"> <input type="text" name="Caption" class="txt" value="@cap"/> </td> <td> <input type="text" name="NameInUse" class="txt" value="@nameinuse"/> </td> <td> <input type="text" name="Description" class="txt" value="@desc"/> </td> <td> @Html.ActionLink("Edit", "Edit", "Organization", new { obj = item as Organization }, null) </td> </tr> 

}

It is raising an exception: The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Edit(Int32)' in 'PartyWeb.Controllers.Internal.OrganizationController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

Can somebody advise how to pass object as parameter?

0

1 Answer 1

3

Can somebody advise how to pass object as parameter?

Why are you using an ActionLink? An ActionLink sends a GET request, not POST. So don't expect your [HttpPost] action to ever be invoked by using an ActionLink. You will have to use an HTML form and include all the properties you want to be sent as input fields.

So:

<tr> <td colspan="4"> @using (Html.BeginForm("Edit", "Organization", FormMethod.Post)) { <table> <tr> @foreach (var item in Model) { <td class="txt"> @Html.TextBox("Caption", item.GetValForProp<string>("Caption"), new { @class = "txt" }) </td> <td class="txt"> @Html.TextBox("NameInUse", item.GetValForProp<string>("NameInUse"), new { @class = "txt" }) </td> <td class="txt"> @Html.TextBox("Description", item.GetValForProp<string>("Description"), new { @class = "txt" }) </td> <td> <button type="submit">Edit</button> </td> } </tr> </table> } </td> </tr> 

Also notice that I used a nested <table> because you cannot have a <form> inside a <tr> and some browser such as IE won't like it.

Sign up to request clarification or add additional context in comments.

2 Comments

One more thing, when I click on edit button, the focus is not going to edit action mentioned above, it is directly going into the model class, can you please advise how can I invoke the edit action I have mentioned above?
+1. I was not aware about the action link functionality. Your post always helps others not only for a specific issues but also for the basic stuff.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.