如何使用 EditorFor 保存对 Model 的更改?
How to use EditorFor to save changes to the Model?
我的看法是这样的:
@model IEnumerable<Namespace.MyClass>
@using Newtonsoft.Json;
<form asp-action="Update">
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Product)
</th>
<th>
@Html.DisplayNameFor(model => model.IsAvailable)
</th>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Product)
</td>
<td>
@Html.EditorFor(modelItem => item.IsAvailable)
</td>
</tr>
}
</tbody>
</table>
<div>
<a href=@Url.Action("SaveChanges", "Product", new {productList = JsonConvert.SerializeObject(Model) })> Save changes</a>
</div>
</form>
SaveChanges 看起来像这样:
[HttpGet] // Perhaps it should be HttpPost here, but if I change it to that nothing happens when I run the program after clicking the "Save changes" link
public ActionResult SaveChanges(string productList)
{
List<MyClass> products = JsonConvert.DeserializeObject<List<MyClass>>(productList);
// Here I continue with SqlCommand to later make an UPDATE to a database changing the value of IsAvailable which is the only adjustable value in my view
}
我的问题是,当我 运行 程序并将 IsAvailable(布尔值)从 false
更改为 true
并按“保存更改”时,模型没有改变,即 productList 对于该特定产品的 IsAvailable 仍然具有值 False。
有人知道为什么吗?根据我的理解,如果我有一个 and
当您将模型发送到具有 link 的操作时,模型信息不会更新。并发送相同的初始金额。
您对模型字段所做的更改仅在 html 控件中,与模型无关。当您 post 表单信息时,模型会更新。
我在下面的代码中提交了表单给jQuery
可见
@model List<Namespace.MyClass>
@using (Html.BeginForm("SaveChanges", "Product", FormMethod.Post))
{
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.FirstOrDefault().Product)
</th>
<th>
@Html.DisplayNameFor(model => model.FirstOrDefault().IsAvailable)
</th>
</thead>
<tbody>
@for (int i = 0; i < Model.Count(); i++)
{
<tr>
<td>
@Html.DisplayFor(modelItem => Model[i].Product)
</td>
<td>
@Html.EditorFor(modelItem => Model[i].IsAvailable)
</td>
</tr>
}
</tbody>
</table>
<div>
@Html.ActionLink("Save changes", "", null, new { @id = "submit"})
</div>
}
@section scripts{
<script type="text/javascript">
$("#submit").click(function () {
document.forms[0].submit();
return false;
});
</script>
}
控制器中的动作
[HttpPost] // Perhaps it should be HttpPost here, but if I change it to that nothing happens when I run the program after clicking the "Save changes" link
public ActionResult SaveChanges(IEnumerable<MyClass> products)
{
// Here I continue with SqlCommand to later make an UPDATE to a database changing the value of IsAvailable which is the only adjustable value in my view
}
我的看法是这样的:
@model IEnumerable<Namespace.MyClass>
@using Newtonsoft.Json;
<form asp-action="Update">
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Product)
</th>
<th>
@Html.DisplayNameFor(model => model.IsAvailable)
</th>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Product)
</td>
<td>
@Html.EditorFor(modelItem => item.IsAvailable)
</td>
</tr>
}
</tbody>
</table>
<div>
<a href=@Url.Action("SaveChanges", "Product", new {productList = JsonConvert.SerializeObject(Model) })> Save changes</a>
</div>
</form>
SaveChanges 看起来像这样:
[HttpGet] // Perhaps it should be HttpPost here, but if I change it to that nothing happens when I run the program after clicking the "Save changes" link
public ActionResult SaveChanges(string productList)
{
List<MyClass> products = JsonConvert.DeserializeObject<List<MyClass>>(productList);
// Here I continue with SqlCommand to later make an UPDATE to a database changing the value of IsAvailable which is the only adjustable value in my view
}
我的问题是,当我 运行 程序并将 IsAvailable(布尔值)从 false
更改为 true
并按“保存更改”时,模型没有改变,即 productList 对于该特定产品的 IsAvailable 仍然具有值 False。
有人知道为什么吗?根据我的理解,如果我有一个 and
当您将模型发送到具有 link 的操作时,模型信息不会更新。并发送相同的初始金额。
您对模型字段所做的更改仅在 html 控件中,与模型无关。当您 post 表单信息时,模型会更新。
我在下面的代码中提交了表单给jQuery
可见
@model List<Namespace.MyClass>
@using (Html.BeginForm("SaveChanges", "Product", FormMethod.Post))
{
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.FirstOrDefault().Product)
</th>
<th>
@Html.DisplayNameFor(model => model.FirstOrDefault().IsAvailable)
</th>
</thead>
<tbody>
@for (int i = 0; i < Model.Count(); i++)
{
<tr>
<td>
@Html.DisplayFor(modelItem => Model[i].Product)
</td>
<td>
@Html.EditorFor(modelItem => Model[i].IsAvailable)
</td>
</tr>
}
</tbody>
</table>
<div>
@Html.ActionLink("Save changes", "", null, new { @id = "submit"})
</div>
}
@section scripts{
<script type="text/javascript">
$("#submit").click(function () {
document.forms[0].submit();
return false;
});
</script>
}
控制器中的动作
[HttpPost] // Perhaps it should be HttpPost here, but if I change it to that nothing happens when I run the program after clicking the "Save changes" link
public ActionResult SaveChanges(IEnumerable<MyClass> products)
{
// Here I continue with SqlCommand to later make an UPDATE to a database changing the value of IsAvailable which is the only adjustable value in my view
}