MVC5不能在一个视图中使用两个模型

时间:2016-05-23 04:21:33

标签: c# asp.net-mvc

Theres已经链接了如何使用不同的方式为视图使用多个模型,但是,我试过这些并且无法使它们工作,我做错了什么?

我只想在1个视图和一个模型中使用两个表单输入,但其中一个表单输入使用列表<' model'>而另一个使用' model',这就是我的意思:

更新:复制/粘贴此代码,如果您选择并提交任何复选框项目,您将在@ Model.input.passWord中收到错误,我不知道为什么,复选框项目也不显示,需要帮助。

View (Index.cshtml):

@using stupidassTests.Models
@model  MyViewModel


@{
ViewBag.Title = "Index";


}

<h2>Password Input</h2>

<div>
<p>Enter Password</p>
@using (Html.BeginForm("Index", "Home", FormMethod.Get))
{
    @Html.TextBox("password")

    <button type="submit" value="Search"></button>
}

<p>@Model.input.passWord</p> <!--passWord is underlined with red because it conflicts with the List'model'-->

</div>

<h2>Checkbox</h2>

<div>

@using (Html.BeginForm())
{
    for (var i = 0; i < Model.inputCollection.Count; i++)
    {
        <p>
            @Html.HiddenFor(n => n.inputCollection[i].Id)
            @Html.DisplayFor(n => n.inputCollection[i].Name)
            @Html.HiddenFor(n => n.inputCollection[i].Name)
            @Html.CheckBoxFor(n => n.inputCollection[i].Checked)
        </p>

    }

    <input id="Submit1" type="submit" value="submit" />


    if (ViewBag.Values != null)
    {
        foreach (var item in ViewBag.Values)
        {
            <p>@item</p>
        }
    }



}

正如您所看到的,复制/粘贴我的代码并尝试运行它,&#39;密码&#39;表格输入被“复选框”推出。输入,似乎是两个&#39; @ model&#39;在一个模型类下是冲突的,我该如何解决这个问题?

Controller(HomeController.cs):

公共ActionResult索引()         {

        return View();
    }



    [HttpGet, ActionName("Index")]
    public ActionResult PasswordInput(string password)
    {
        FormInputs pss = new FormInputs();

        pss.passWord = password;

        MyViewModel mvm = new MyViewModel() { input = pss, isList = false };

return this.View("Index", mvm);

    }





    [HttpGet]
    public ActionResult CheckBoxGet()
    {


        var list = new List<FormInputs>
        {
             new FormInputs { Id = 1, Name = "Aquafina", Checked = false },
        new FormInputs { Id = 2, Name = "Mulshi Springs", Checked = false },
         new FormInputs { Id = 3, Name = "Alfa Blue", Checked = false },
         new FormInputs { Id = 4, Name = "Atlas Premium", Checked = false },
         new FormInputs { Id = 5, Name = "Bailley", Checked = false },
         new FormInputs { Id = 6, Name = "Bisleri", Checked = false },
         new FormInputs { Id = 7, Name = "Himalayan", Checked = false },
         new FormInputs { Id = 8, Name = "Cool Valley", Checked = false },
         new FormInputs { Id = 9, Name = "Dew Drops", Checked = false },
         new FormInputs { Id = 10, Name = "Dislaren", Checked = false },

 };
        MyViewModel mvm = new MyViewModel() { inputCollection = list, isList = true };
return this.View("Index", mvm);

    }


    [HttpPost]
    public ActionResult CheckBoxPost(List<FormInputs> list)
    {


        var selected = list.Where(x => x.Checked).Select(x => x.Name);

        ViewBag.Values = selected;

        MyViewModel mvm = new MyViewModel() { inputCollection = list, isList = true };

        return this.View("Index", mvm);
    }

Model(FormInputs.cs):

public class MyViewModel
{
    public FormInputs input;
    public List<FormInputs> inputCollection;
    public bool isList;
}
public class FormInputs
{
    public string passWord = "";


    public int Id { get; set; }
    public string Name { get; set; }
    public bool Checked { get; set; }

    public List<string> checkBox = new List<string>();


}

所以,作为一个总结,因为我是MVC的初学者,我如何重新处理这段代码(顺便说一下复制/粘贴它),以便两个表单输入可以在1个视图中共存?

2 个答案:

答案 0 :(得分:1)

您可以使用viewmodel。

使用ViewModel

对于视图模型,您必须创建一个类,在此类中,您将所有模型定义为此类的属性。这是两个类。

public class ViewModel
{
    public Employee emp { get; set; }
    public EmployeeDetails empdet{ get; set; }
}

这是viewmodel

public ActionResult About()
{
        ViewModel vm = new ViewModel();
        vm.emp = new Employee();
        vm.empdet = new EmployeeDetails();
        return View(vm);
}

现在在Controller中你会这样做

@model ViewModel

在视野中,你会像这样收到它

{{1}}

答案 1 :(得分:1)

这可能是使用Composite Pattern的一个很好的例子。您可以拥有一个具有两个属性的ViewModel:

public class MyViewModel{
    public FormInputs input;
    public List<FormInputs> inputCollection;
    public bool isList;
}

并相应地安排数据:

public ActionResult PasswordInput(string password)
{
    FormInputs pss = new FormInputs();

    pss.passWord = password;

    MyViewModel mvm = new MyViewModel(){input = pss, isList = false}
    return this.View("Index", mvm); 
}

public ActionResult CheckBoxGet()
{
    var list = new List<FormInputs>
    {
         new FormInputs { Id = 1, Name = "Aquafina", Checked = false },
    new FormInputs { Id = 2, Name = "Mulshi Springs", Checked = false },
     new FormInputs { Id = 3, Name = "Alfa Blue", Checked = false },
     new FormInputs { Id = 4, Name = "Atlas Premium", Checked = false },
     new FormInputs { Id = 5, Name = "Bailley", Checked = false },
     new FormInputs { Id = 6, Name = "Bisleri", Checked = false },
     new FormInputs { Id = 7, Name = "Himalayan", Checked = false },
     new FormInputs { Id = 8, Name = "Cool Valley", Checked = false },
     new FormInputs { Id = 9, Name = "Dew Drops", Checked = false },
     new FormInputs { Id = 10, Name = "Dislaren", Checked = false },

    };
    MyViewModel mvm = new MyViewModel(){inputCollection = list , isList = true}
    return this.View("Index", mvm); 

}

在视图中,使用此:

@model MyViewModel

在使用input / inputCollection

之前检查isList属性