MVC更新表格数据POST的各个行

时间:2016-12-13 17:03:01

标签: c# asp.net-mvc

我只显示几行数据,每行都有相同的输入字段和“更新”字样。提交按钮。

我有点担心如何通过POST更新每一行数据?

到目前为止,我已经有了一个观点

@model IEnumerable <PGS.Areas.Case.Models.SurveyorsOfferModel>
<table>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                using (Html.BeginForm("EditSurveyor", "ValuationsAndOffers", new { id = @item.Id }, FormMethod.Post))
                {
                <table class="table">
                    <thead>
                        <tr>
                            <th>Surveyor</th>
                            <th>Date</th>
                            <th>M.V.</th>
                            <th>4-6 Week</th>
                            <th>Valuation to Use</th>
                            <th>Use</th>
                            <th>Report Received</th>
                            <th width="70"></th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>
                                @Html.HiddenFor(model => item.Id)
                                @Html.DisplayFor(model => item.SupplierName)
                            </td>
                            <td>
                                @Html.EditorFor(model => item.ValuationDateTime, new { htmlAttributes = new { @class = "form-control datepicker" } })
                            </td>
                            <td>
                                @Html.EditorFor(model => item.Valuation1, new { htmlAttributes = new { @class = "form-control currency_input" } })
                            </td>
                            <td>
                                @Html.EditorFor(model => item.Valuation2, new { htmlAttributes = new { @class = "form-control currency_input" } })
                            </td>
                            <td>
                                @Html.DropDownList("ValuationToUse", item.ValuationToUseList, new { @class = "form-control small" })
                            </td>
                            <td>
                                @Html.EditorFor(model => item.UseInOffer, new { htmlAttributes = new { @class = "form-control chk" } })
                            </td>
                            <td>
                                @Html.EditorFor(model => item.ReportReceivedDate, new { htmlAttributes = new { @class = "form-control datepicker" } })
                            </td>
                            <td>
                                <input id="btnSubmit_@item.Id" type="submit" value="Update" class="btn btn-info" />
                            </td>
                        </tr>
                    </tbody>

                </table>
                }
            </td>
        </tr>
    }
</table>

我已经把每个&#34; Row&#34;在它自己的表中,所以我可以将每个包装在一个表单中,但是当我提交一行时,模型值总是为空。

所以不确定我是否还需要做其他事情来获取特定行的提交数据?

控制器

[HttpPost]
public ActionResult EditSurveyor(SurveyorsOfferModel model)
{
    try
    {
        SurveyorsOffer c = efContext.SurveyorsOffers.Where(x => x.Id == model.Id).Single<SurveyorsOffer>();
        c.ValuationDateTime = model.ValuationDateTime;
        c.Valuation1 = model.Valuation1;
        c.Valuation2 = model.Valuation2;
        c.ValuationUsed = model.ValuationUsed;
        c.UseInOffer = model.UseInOffer;
        c.ReportRecievedDate = model.ReportReceivedDate;

        efContext.SubmitChanges();

        return RedirectToAction("Index", "ValuationsAndOffers", new { id = model.CaseId });
    }
    catch (Exception ex)
    {
        ModelState.AddModelError(string.Empty, ex.Message);
    }

    return View("Index", "~/Views/Shared/_Case_Template.cshtml", model);

}

模型

public class SurveyorsOfferModel
    {
        public int Id { get; set; }
        public int CaseId { get; set; }
        public int SupplierId { get; set; }
        public string SupplierName { get; set; }
        public string SupplierDetails { get; set; }
        public DateTime? ValuationDateTime { get; set; }
        public decimal? Valuation1 { get; set; }
        public decimal? Valuation2 { get; set; }
        public string ValuationUsed { get; set; }
        public bool UseInOffer { get; set; }
        public DateTime? ReportReceivedDate { get; set; }
        public string ContactName { get; set; }
        public string ContactEmail { get; set; }
        public DateTime? DateAdded { get; set; }
        public IEnumerable<SelectListItem> ValuationToUseList { get; set; }
    }

呈现表单HTML

<form action="/Case/ValuationsAndOffers/EditSurveyor/2654" method="post">
<table class="table">
                <thead>
                    <tr>
                        <th>Surveyor</th>
                        <th>Date</th>
                        <th>M.V.</th>
                        <th>4-6 Week</th>
                        <th>Valuation to Use</th>
                        <th>Use</th>
                        <th>Report Received</th>
                        <th width="70"></th>
                    </tr>
                </thead>
                <tbody>
                <tr>
                    <td>
                        <input data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." id="item_Id" name="item.Id" type="hidden" value="2654" />
                        Allen &amp; Harris
                    </td>
                    <td>
                        <input class="form-control datepicker text-box single-line" data-val="true" data-val-date="The field ValuationDateTime must be a date." id="item_ValuationDateTime" name="item.ValuationDateTime" type="datetime" value="" />
                    </td>
                    <td>
                        <input class="form-control currency_input text-box single-line" data-val="true" data-val-number="The field Valuation1 must be a number." id="item_Valuation1" name="item.Valuation1" type="text" value="" />
                    </td>
                    <td>
                        <input class="form-control currency_input text-box single-line" data-val="true" data-val-number="The field Valuation2 must be a number." id="item_Valuation2" name="item.Valuation2" type="text" value="" />
                    </td>
                    <td>
                        <select class="form-control small" id="ValuationToUse" name="ValuationToUse"><option value="MV">M.V.</option>
    <option value="4_6">4-6 Week</option>
    </select>

                    </td>
                    <td>
                        <input class="form-control chk check-box" data-val="true" data-val-required="The UseInOffer field is required." id="item_UseInOffer" name="item.UseInOffer" type="checkbox" value="true" /><input name="item.UseInOffer" type="hidden" value="false" />
                    </td>
                    <td>
                        <input class="form-control datepicker text-box single-line" data-val="true" data-val-date="The field ReportReceivedDate must be a date." id="item_ReportReceivedDate" name="item.ReportReceivedDate" type="datetime" value="" />
                    </td>
                    <td>

                        <input id="btnSubmit_2654" type="submit" value="Update" class="btn btn-info" />
                    </td>
                </tr>
                </tbody>

            </table>
    </form>

3 个答案:

答案 0 :(得分:1)

您的问题是模型需要是一个单独的项而不是枚举。

父视图:

@model IEnumerable <PGS.Areas.Case.Models.SurveyorsOfferModel>
<table>
    @foreach (var item in Model)
    {
        <tr>
            <td>
               @Html.Partial("ChildView",item)
            </td>
        </tr>
    }
</table>

ChildView:

@model PGS.Areas.Case.Models.SurveyorsOfferModel
using (Html.BeginForm("EditSurveyor", "ValuationsAndOffers", new { id = @Model.Id }, FormMethod.Post))
{
    <table class="table">
        <thead>
            <tr>
                <th>Surveyor</th>
                <th>Date</th>
                <th>M.V.</th>
                <th>4-6 Week</th>
                <th>Valuation to Use</th>
                <th>Use</th>
                <th>Report Received</th>
                <th width="70"></th>
           </tr>
        </thead>
        <tbody>
           <tr>
                <td>
                   @Html.HiddenFor(model => model.Id)
                   @Html.DisplayFor(model => model.SupplierName)
                </td>
                <td>
                   @Html.EditorFor(model => model.ValuationDateTime, new { htmlAttributes = new { @class = "form-control datepicker" } })
                </td>
                ... 
                <td>
                   <input id="btnSubmit_@Model.Id" type="submit" value="Update" class="btn btn-info" />
                </td>
           </tr>
        </tbody>

    </table>
}

答案 1 :(得分:1)

绑定与name属性匹配。这将绑定到模型的Id属性:

<input name="Id" value="1" type="text" />

您的视图包含您迭代的集合。以这种方式使用表单助手会生成不会绑定的名称属性。

<input name="item.Id" value="" type="text" />

覆盖名称属性

EditorFor()不允许您覆盖名称属性,因此请改用TextBoxFor()

@Html.TextBoxFor(m => item.Id, new { @Name="Id" })

注意名称

的大小写

改用模型

如果您将变量名称从item更改为model,它也会有效。

@Html.EditorFor(m => model.Id)

生成

<input name="Id" id="Id" type="text" />

但这对我来说感觉神奇而且惹人讨厌。它还会强迫你在foreach循环中使用“model”。

答案 2 :(得分:0)

只需添加一条注释,以防其他人遇到类似的事情。

BK2的答案是正确的,并帮助我让它工作,但我遇到问题(我想我可能)与表单元素有重复的Id,所以我必须为每个表单项设置Id和Name属性, Id是独一无二的。

@Html.EditorFor(model => model.ReportReceivedDate, new { htmlAttributes = new { @id = "ReportReceivedDate" + @Model.SurveyorOfferId, @name = "ReportReceivedDate", @class = "form-control datepicker", @Value = string.Format("{0:dd-MM-yyyy}", @Model.ReportReceivedDate) } })