将数据发布到多个表MVC EF6

时间:2015-11-30 16:25:51

标签: jquery database model-view-controller one-to-many

在我的MVC应用程序中,我有一个表单,我想将数据发布到多个表。我想知道如何实现这一目标? 这是我的控制器的代码以及我的视图的代码。

控制器

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "id_index,createddate,details,options")] tbl_info tbl_info)
    {
        if (ModelState.IsValid)
        {
            db.tbl_info.Add(tbl_info);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(tbl_info);
    }

查看

@using (Html.BeginForm()) 
{@Html.AntiForgeryToken()

<div class="form-horizontal">
    <h4>The Form</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.createddate, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.createddate, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.createddate, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.details, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.TextAreaFor(model => model.details, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.details, "", new { @class = "text-danger" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(model => model.options, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.TextAreaFor(model => model.options, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.options, "", new { @class = "text-danger" })
        </div>
    </div>

用户可以添加许多选项(可以通过jquery克隆选项输入),我需要将这些选项发布到与tbl_info具有一对多关系的单独表中。有谁知道如何做到这一点?

1 个答案:

答案 0 :(得分:0)

您的模型需要设置如下:

public class tbl_info
{
  public int tbl_infoID {get; set;}
  public string details {get; set;}
  etc ...

  public virtual ICollection<tbl_options> Options {get; set;}
}

public class tbl_options
{
  public int id {get; set;}
  public int tbl_infoID {get; set;}
  public string option_value {get; set;}
}

然后在您的视图中,您必须为每个选项命名,如Model.Options [0] .option_value。你必须递增[0]; [0],[1]等。您不能跳过任何数字,它必须从0开始,因此MVC可以将您的表单对象与您的选项模型相关联。

<div class="form-group">
    @Html.LabelFor(model => model.options, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.TextAreaFor(model => model.Options[0].option_value, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.Options[0].option_value, "", new { @class = "text-danger" })
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(model => model.options, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.TextAreaFor(model => model.Options[1].option_value, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.Options[1].option_value, "", new { @class = "text-danger" })
    </div>
</div>

****修改

您的控制器应如下所示:

[HttpPost]
public ActionResult Create(tbl_info myModel)
{
  if (ModelState.IsValid)
  {
    db.tbl_info.Add(myModel);
    db.SaveChanges();
    return RedirectToAction("Index");
  }
  return View(myModel);
}