[英]MVC5 passing a Model item from View to a Controller
I am using .Net MVC5 and I am trying to create an Index view with an Edit Link(or button) that will POST(so I can't use ActionLink) an entire Model item Entity from the list of entities presented in the View. 我正在使用.Net MVC5,我正在尝试创建一个带有编辑链接(或按钮)的索引视图,该视图将在视图中显示的实体列表中POST(因此我无法使用ActionLink)整个模型项实体。 How do I do it? 我该怎么做?
my code(so far) is below 我的代码(到目前为止)如下
@model IEnumerable<Projname.Models.MyEntity>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>Hello there</th>
<th>life is good</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@using (Html.BeginForm("Edit", "Edit", FormMethod.Post))
{
@Html.Hidden(mytem => item);
<input type="submit" value="Edit" class="btn btn-default" />
}
</td>
<td>
@Html.ActionLink("Details", "Details", new { id = item.PrimeKey }) |
@Html.ActionLink("Delete", "Delete", new { id = item.PrimeKey })
</td>
</tr>
}
</table>
</body>
</html>
I am assuming that there is a better way to do it instead of creating a hidden field for each of the properties of the entity. 我假设有一种更好的方法来实现它,而不是为实体的每个属性创建一个隐藏字段。
You need to put the items you want to edit or change into a form 您需要将要编辑或更改的项目放入表单中
<form class="form-horizontal" action="GetItemsFromView" method="post">
<input type="text" name="EditedModelItem">
</form>
Afterwards you can call any edited item in your controller, through string hold = Request.Form["EditedModelItem"]; 之后,您可以通过string hold = Request.Form [“EditedModelItem”]调用控制器中的任何已编辑项目; in the GetItemsFromView method. 在GetItemsFromView方法中。
Wrap everything in: 包裹所有内容:
@using (Html.BeginForm("Edit", "Edit", FormMethod.Post))
{
}
Then: 然后:
public ActionResult Edit(Model model)
{
if (ModelState.IsValid)
{
}
}
Or: 要么:
public ActionResult Edit(FormCollection formCollection)
{
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.