MVC中的POST形式-没有值写入数据库

时间:2018-10-02 15:15:11

标签: asp.net-mvc

我有一个要使用表单编辑的模型:

public class Basiclife
{
    [Key]
    public int Id { get; set; }
    public int? ResponseId { get; set; }
    public string Plantype { get; set; }
    public int Enrolledftes { get; set; }
    public decimal Pctemployer { get; set; }
    public decimal Fixedbenamt { get; set; }
    public decimal Salarymult { get; set; }
    public decimal Bencap { get; set; }
}

以及用于对其进行编辑的视图包装器(编辑器位于单独的局部视图中):

<h2>CreateBasicLifeResponse</h2>
<div id="planList">
    @using (Html.BeginForm("CreateBasicLifeResponse", "Surveys"))
    {
        <div id="editorRows">
            @foreach (var item in Model.basiclives)
            {
                @Html.Partial("_BasicLifeResponse", item)
            }
        </div>
        @Html.ActionLink("Add", "BasicLifeResponse", null, new { id = "addItem", @class = "button" });
        <input type="submit" value="submit" />
    }

</div>

包装器的模型是:

public class ResponseBasicLife
{
    public Response response { get; set; }
    public List<Basiclife> basiclives { get; set; }
}

这是局部视图:

@using CustomSurveyTool.Models
@model Basiclife

<div class="editorRow">
    @using (Html.BeginCollectionItem("basiclives"))
    {
    <div class="form-horizontal">


        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.Plantype, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Plantype, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Plantype, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Enrolledftes, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Enrolledftes, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Enrolledftes, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Pctemployer, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Pctemployer, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Pctemployer, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Fixedbenamt, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Fixedbenamt, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Fixedbenamt, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Salarymult, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Salarymult, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Salarymult, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Bencap, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Bencap, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Bencap, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            <a href="#" class="deleteRow">X</a>
        </div>
    </div>
    }
</div>

这是我的控制器操作,在该操作中,我获得了正确的responseId并将其分配给表单值:

         public ActionResult CreateBasicLifeResponse(ResponseBasicLife model)
    {

        for (var i = 1; i < model.basiclives.Count; i++)
        {
            string currentUserId = User.Identity.GetUserId();
            Response targetresponse = db.response.FirstOrDefault(x => x.Userid == currentUserId);
            int responseid = targetresponse.Id;
            model.basiclives[i].ResponseId = responseid;
            db.basiclife.Add(model.basiclives[i]);
            db.SaveChanges();
        }

        ResponseBasicLife basicliferesponse = new ResponseBasicLife
        {
            basiclives = new List<Basiclife>
            {
                new Basiclife {  }
            }
        };
        return View(basicliferesponse);
    }

唯一要写入数据库的是ResponseID。如何获取其余的值写入其中?

1 个答案:

答案 0 :(得分:0)

这是一种独特的情况,EditorFor可以提供帮助。本质上,对象将具有其编辑器模板,该模板有点像局部视图,但特定于编辑表单。

第二,您正在指定的being表单期望您所指定的视图的模型-在您的情况下为viewWrapper。例如,如果您指定了List<BasicLife>,那么postBack应该是预期的,而不是BasicLife对象。以下是您的回发外观。

[HttpPost]
public ActionResult CreateBasicLifeResponse(List<BasicLife> model){
      //your code goes here
}

不过,从您的角度来看,很明显Model不仅包含列表。那是回调所期望的。