C# 具有加载和表单模型的MVC视图
我有一个像表单一样工作的视图,因此它加载一组下拉列表,并从从C# 具有加载和表单模型的MVC视图,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,我有一个像表单一样工作的视图,因此它加载一组下拉列表,并从从ActionResult Index中的控制器返回的FormViewModel类中进行选择。将所有数据输入表单后,我使用Html.BeginForm(“Create”调用save方法。问题是我想使用两个模型,一个用于加载,另一个用于保存数据。我现在要做的是从FormViewModel和RequestForm模型中导入对象 有更好的方法吗 视图: 控制器 public ActionResult Create(RequestForm mod
ActionResult Index
中的控制器返回的FormViewModel类中进行选择。将所有数据输入表单后,我使用Html.BeginForm(“Create”
调用save方法。问题是我想使用两个模型,一个用于加载,另一个用于保存数据。我现在要做的是从FormViewModel和RequestForm模型中导入对象
有更好的方法吗
视图:
控制器
public ActionResult Create(RequestForm model)
{
var FormInfo = FormCreate(model);
return View("");
}
您可以使用两个模型(一个用于加载表单内容,另一个用于将选择、输入发送到服务器)。模型绑定为您完成这项工作,即当到达控制器类的操作方法(创建)时,它将输入值转换为模型类(请求表单)
根据您共享的信息,这种方法似乎还可以您可以嵌套查看模型,只在表单post上提交所需的模型 例如:
公共类FormViewModel
{
public IEnumerable AvailableCountries{get;set;}
公共CreateRequestViewModel数据{get;set;}
}
公共类CountryViewModel
{
public int CountryId{get;set;}
公共字符串CountryName{get;set;}
}
公共类CreateRequestViewModel
{
[必需]
public int SelectedCountryId{get;set;}
}
我只是编了几个名字,但希望你能明白
然后在视图上,您可以按如下方式进行设置:
@model FormViewModel
@using (Html.BeginForm("Create", "RequestForm", FormMethod.Post))
{
@Html.ValidationSummary(true);
<div class="k-content">
<h4>* Country</h4>
@Html.DropDownListFor(
x => x.Data.SelectedCountryId,
new SelectList(Model.AvailableCountries, "CountryId", "CountryName"),
new { id = "ddlCountry", @class = "form-control" }
)
@Html.ValidationMessageFor(x => x.Data.SelectedCountryId)
</div>
}
参数的名称必须与外部模型中声明的名称相匹配,
FormViewModel
您可以使用一个视图模型在视图中加载数据,并使用它将数据发布到服务器,然后在服务器上,您可以将模型转换为将保存到数据库的新实体
@model FormViewModel
@using (Html.BeginForm("Create", "RequestForm", FormMethod.Post))
{
@Html.ValidationSummary(true);
<div class="k-content">
<h4>* Country</h4>
@Html.TextBoxFor(x => x.CountryId, new { id = "ddlCountry", @class = "form-control" })
@Html.ValidationMessageFor(model => model.CountryId)
</div>
}
控制器
public ActionResult Create()
{
// you can use this method in edit mode too by loading the data to model
FormViewModel model = new FormViewModel() ;
return View(model) ;
}
[HttpPost]
public ActionResult Create(FormViewModel model)
{
//convert the model to the DB entity may be you can use automapper
var entity = converted model;
// save to DB
return View(model) ;
}
我试着尽可能地解释代码你能告诉我们你有什么,你想要什么,错误是什么吗?你有没有研究过其他SO答案?看起来很有希望Anks@muhammad waqas iqbal你需要两个模型中的对象吗?@Jefferson,嗯,一个模型(FormViewModel)将用于填充示例中的国家/地区列表,而另一个(RequestForm)将用作获取提交表单时用户选择的CountryId的参数
@model FormViewModel
@using (Html.BeginForm("Create", "RequestForm", FormMethod.Post))
{
@Html.ValidationSummary(true);
<div class="k-content">
<h4>* Country</h4>
@Html.DropDownListFor(
x => x.Data.SelectedCountryId,
new SelectList(Model.AvailableCountries, "CountryId", "CountryName"),
new { id = "ddlCountry", @class = "form-control" }
)
@Html.ValidationMessageFor(x => x.Data.SelectedCountryId)
</div>
}
@model FormViewModel
@using (Html.BeginForm("Create", "RequestForm", FormMethod.Post))
{
@Html.ValidationSummary(true);
<div class="k-content">
<h4>* Country</h4>
@Html.TextBoxFor(x => x.CountryId, new { id = "ddlCountry", @class = "form-control" })
@Html.ValidationMessageFor(model => model.CountryId)
</div>
}
[Required]
public int? CountryId { get; set; }
public List<CountryModel> ListOfCountries { get; set; }
public int? CountryId { get; set; }
public ActionResult Create()
{
// you can use this method in edit mode too by loading the data to model
FormViewModel model = new FormViewModel() ;
return View(model) ;
}
[HttpPost]
public ActionResult Create(FormViewModel model)
{
//convert the model to the DB entity may be you can use automapper
var entity = converted model;
// save to DB
return View(model) ;
}