模型结合了多个模型,总是向控制器发送空

时间:2018-04-17 15:47:28

标签: c# asp.net-mvc

我有一个模型,它结合了多个模型总是被发送到控制器作为空模型

模型

namespace Test.Models
{
    public class Compination
    {
        public firstmodel fmodel{ get; set; }
        public secondmodel smodel{ get; set; }

    }
}

查看

@model Test.Models.Compination
@using (Html.BeginForm("Edit", "Home", FormMethod.Post)){
<div>
@Html.LabelFor(model => model.firstmodel.Name, "Name")
@Html.EditorFor(model => model.firstmodel.Name)
</div>
   //other fields
}

控制器

public ActionResult Edit(int? id){
     firstmodel FM= db.firstmodel.Find(id);
     ClassA object = methodA(id);
     secondmodel SM = object.secondmodel; 
   var Compination= new Compinati
on{ firstmodel= FM, secondmodel = S};

            return View(Compination);
}

通过这种方式,名称例如为空

public ActionResult Edit(Compination comp){
 var name=   comp.firstmodel.Name;
 //other code }

但如果我为每个模型中的每个项目指定了名称,则会正确传递值

@Html.EditorFor(model => model.firstmodel.Name, new { htmlAttributes = new { @Name = "Name" })

然后在控制器中

public ActionResult Edit(string name){
     // Now name isn't empty
     //other code }

我不想单独调用每个项目我想要整个模型

0 个答案:

没有答案