Reputation: 1507
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?
Upvotes: 0
Views: 4246
Reputation: 1039408
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.
Upvotes: 3