繁体   English   中英

如何使用 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并按“保存更改”时,model 不会改变,即 productList 的 IsAvailable 的值仍然为 False特定产品。

有人知道为什么吗? 据我了解,如果我有一个和

当您通过链接将 model 发送到操作时,Model 信息不会更新。 并且发送相同的初始金额。

您对 model 字段所做的更改仅在 html 控件中,与 model 无关。 model 会在您发布表单信息时更新。

我在以下代码中将表格提交给 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>
}

controller 中的动作

[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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM