Asp.net mvc 4 Html.ActionLink对象参数+;MVC4
我正在从视图中调用编辑操作,该操作应接受对象作为参数 行动:Asp.net mvc 4 Html.ActionLink对象参数+;MVC4,asp.net-mvc-4,razor,Asp.net Mvc 4,Razor,我正在从视图中调用编辑操作,该操作应接受对象作为参数 行动: [HttpPost] public ActionResult Edit(Organization obj) { //remove the lock since it is not required for inserts if (ModelState.IsValid) { OrganizationRepo.Update(obj);
[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();
}
}
从视图:
@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>
@foreach(模型中的变量项){
cap=项目。GetValForProp(“标题”);
nameinuse=item.GetValForProp(“nameinuse”);
desc=项目。GetValForProp(“说明”);
@ActionLink(“编辑”、“编辑”、“组织”,新建{obj=item as Organization},null)
}
它引发了一个异常:参数字典包含“PartyWeb.Controllers.Internal.OrganizationController”中方法“System.Web.Mvc.ActionResult Edit(Int32)”的非null类型“System.Int32”的参数“id”的null条目。可选参数必须是引用类型、可为null的类型或声明为可选参数。
参数名称:参数
有人能建议如何将对象作为参数传递吗
有人能建议如何将对象作为参数传递吗
为什么要使用ActionLink?ActionLink发送GET请求,而不是POST。因此,不要指望您的[HttpPost]
操作会被ActionLink调用。您必须使用HTML表单,并包含所有要作为输入字段发送的属性
因此:
@使用(Html.BeginForm(“编辑”、“组织”、FormMethod.Post))
{
@foreach(模型中的var项目)
{
@TextBox(“Caption”,item.GetValForProp(“Caption”),new{@class=“txt”})
@TextBox(“NameInUse”,item.GetValForProp(“NameInUse”),new{@class=“txt”})
@TextBox(“Description”,item.GetValForProp(“Description”),new{@class=“txt”})
编辑
}
}
还请注意,我使用了嵌套的
,因为
中不能有
,某些浏览器(如IE)不喜欢它
有人能建议如何将对象作为参数传递吗
为什么要使用ActionLink?ActionLink发送GET请求,而不是POST。因此,不要指望您的[HttpPost]
操作会被ActionLink调用。您必须使用HTML表单,并包含所有要作为输入字段发送的属性
因此:
@使用(Html.BeginForm(“编辑”、“组织”、FormMethod.Post))
{
@foreach(模型中的var项目)
{
@TextBox(“Caption”,item.GetValForProp(“Caption”),new{@class=“txt”})
@TextBox(“NameInUse”,item.GetValForProp(“NameInUse”),new{@class=“txt”})
@TextBox(“Description”,item.GetValForProp(“Description”),new{@class=“txt”})
编辑
}
}
还请注意,我使用了嵌套的
,因为在
中不能有
,某些浏览器(如IE)不喜欢它。还有一件事,当我单击编辑按钮时,焦点不是上面提到的编辑操作,而是直接进入模型类,您能告诉我如何调用上面提到的编辑操作吗?+1。我不知道action link的功能。你的帖子总是帮助别人,不仅是针对某个特定的问题,还有一些基本的东西。还有一件事,当我点击编辑按钮时,焦点不是上面提到的编辑操作,而是直接进入模型类,你能告诉我如何调用上面提到的编辑操作吗?+1。我不知道action link的功能。你的帖子总是帮助别人,不仅是在某个特定的问题上,而且是在基本的东西上。
<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>