gpt4 book ai didi

c# - MVC5 不能在一个 View 中使用两个模型

转载 作者:行者123 更新时间:2023-11-30 23:25:18 26 4
gpt4 key购买 nike

已经有关于如何通过不同的方式将多个模型用于一个 View 的链接,但是,我尝试了这些但无法让它们工作,我做错了什么?

我只想在 1 个 View 和一个模型中使用两个表单输入,但其中一个表单输入使用列表<'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>
}
}



}

如您所见,复制/粘贴我的代码并尝试运行它,“密码”表单输入被“复选框”输入挤掉,似乎两个“@model”在一个模型类下发生冲突,我该如何解决这个问题?

Controller (HomeController.cs):

公共(public) Action 结果索引() {

        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);
}

模型(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 的初学者,我该如何重新编写这段代码(顺便说一句,复制/粘贴它),以便两个表单输入可以在一个 View 中共存?

最佳答案

您可以使用 View 模型。

使用 View 模型

对于 View 模型,你必须创建一个类,在这个类中,你将把所有模型定义为这个类的属性。这里有两个类。

public class EmployeeDetails
{
[Required]
[Display(Name = "Name")]
public string Name { get; set; }

}

public class Employee
{
public int Id { get; set; }
}

这是 View 模型

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

现在在 Controller 中你会这样做

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

在你看来你会这样收到它

@model ViewModel

关于c# - MVC5 不能在一个 View 中使用两个模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37382606/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com