2016-11-28 76 views
0

我有一个剃刀视图,有表格数据。当我提交表单时,我想在“表单”模型中输入一列(ID),并将整个表单数据传递给“初始化”模型。mvc剃刀贴到2个模型

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

<div class="form-horizontal"> 

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

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

      @Html.EditorFor(model => model.name, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.name, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

     </div> 
    </div> 

模型1:初始化

public partial class Init 
{ 
    public int Id { get; set; } 
    public string name{ get; set; } 
} 

模型2:表

public partial class Form 
{ 
    public int Id { get; set; } 
    public int field2 { get; set; } 
    public int field3 { get; set; } 
} 

控制器:

public ActionResult Create([Bind(Include = "Id,Name")] Init init) 
{ 

    if (ModelState.IsValid) 
    { 
     db.Init.Add(init); 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 
    return View(init); 
} 
+0

在视觉studiio,当你创建一个视图,选择与模型强类型,到你想要的数据的选项。 –

+0

我拥有的视图强制输入到Init模型,并在提交它时将数据添加到Init模型。我该如何将表单中的Id值添加到另一个模型(表单模型)? – user7221204

+0

什么是“DocId”,“供应商”,“供应商文字”?我没有看到属性不在模型中,也不在视图中。 – adamshakhabov

回答

1

只需创建一个输入标签用的每一个的唯一的名称您的模型的项目,并让模型联编程序执行它工作。

在下面的示例中,Id属性被传递给modelA和modelB。

视图模型

public class TestViewModel 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
} 

模型

public class TestModelA 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
} 
public class TestModelB 
{ 
    public int Id { get; set; } 
    public string Foo { get; set; } 
    public string Bar { get; set; } 
} 

控制器

[HttpGet] 
    public ActionResult Index() 
    { 
     return View(); 
    } 

    [HttpPost] 
    public ActionResult Index(TestModelA modelA, TestModelB modelB) 
    { 
     return View(); 
    } 

查看

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

<div class="form-horizontal"> 

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

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

      @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" }) 
     </div> 
    </div> 

    <div class="form-group"> 
     <label for="foo" class="control-label col-md-2">Foo</label> 
     <div class="col-md-10"> 
      <input type="text" name="foo" id="foo" class="form-control text-box single-line" /> 
     </div> 
    </div> 

    <div class="form-group"> 
     <label for="bar" class="control-label col-md-2">Bar</label> 
     <div class="col-md-10"> 
      <input type="text" name="bar" id="bar" class="form-control text-box single-line" /> 
     </div> 
    </div> 

    <input type="submit" /> 

</div> 

}

0

我在想你想把细节保存到“Init”中。一旦它被保存过的“初始化”的ID值,并将其保存到“表”示范

首先创建

“初始化”模式和“表”模型之间的关系添加以下属性中的“表”模型

public int initID 

我没有测试这一个,但我认为它应该帮助

 Init Initobj = new Init 
     { 
      Id = Id , 
      DocId= DocId, 
      SupplierText=SupplierText 
      //other properties 
     }; 
     db.Init.Add(Initobj);  
     db.SaveChanges(); 

     Form formobj = new Form 
     { 
      initID= Initobj.Id, 

     }; 
     db.forms.Add(formobj);  
     db.SaveChanges(); 

您可以检查此链接以供参考 Link for reference

0

您应该只是继续:

public ActionResult Create([Bind(Include = "Id, name")] Init init, 
          [Bind(Include = "Id")] Form form) 
{ 

    if (ModelState.IsValid) 
    { 
     db.Init.Add(init); 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 
    return View(init); 
}