MVC视图与加载和窗体模型



我有一个像窗体一样工作的视图,所以它加载了一堆下拉列表,并从ActionResult Index中的Controllers返回的FormViewModel类中进行选择。一旦所有数据都输入到表单中,我就使用Html.BeginForm("Create"来调用save方法。ActionResult Create(RequestForm model)。所以问题是我想使用两个模型,一个用于加载,另一个用于保存数据。我现在要做的是从FormViewModel和RequestForm模型中获取对象。

有更好的方法吗?

查看:

@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>
}

FormViewModel

[Required]
public int? CountryId { get; set; }
public List<CountryModel> ListOfCountries { get; set; }

请求表单模型

public class RequestForm
{
[Required]
public int? CountryId { get; set; }
}

控制器

public ActionResult Create(RequestForm model)
{
var FormInfo = FormCreate(model);
return View("");
}

您可以嵌套视图模型,并且只在表单post上提交您想要的视图模型。

例如:

public class FormViewModel
{
public IEnumerable<CountryViewModel> AvailableCountries { get; set; }
public CreateRequestViewModel Data { get; set; }
}
public class CountryViewModel
{
public int CountryId { get; set; }
public string CountryName { get; set; }
}
public class CreateRequestViewModel
{
[Required]
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>
}

再说一遍,我是手工写的,所以可能会有编译错误。但我们的想法是使用DropDownListFor()而不是TextBoxFor()来生成一个下拉列表,其中包含由您填充的AvailableCountries列表生成的选项。

您只需要将[Required]放在要向其发布数据的视图模型上,因为这是在验证用户的数据。你不需要把它放在CountryViewModel上,因为你自己在填充列表。


最后,还有一件重要的需要注意,那就是表单发布到的方法上的参数名称:

public ActionResult Create(CreateRequestViewModel data)
{
...
}

参数的名称必须与您在外部模型FormViewModel中声明的名称匹配

您可以使用两个模型(一个用于加载表单内容,另一个用于将选择和输入发送到服务器(。Model Binding为您完成了这项工作,即当它到达Controller类的Action方法(Create(时,它将输入的值转换为Model类(RequestForm

根据你分享的信息,这种方法似乎可以

您可以使用一个视图模型加载视图中的数据,并使用它将数据发布到服务器,然后在服务器上,您可以将模型转换为将保存到数据库的新实体。

@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>
}

FormViewModel

[Required]
public int? CountryId { get; set; }
public List<CountryModel> ListOfCountries { get; set; }

DB实体

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

我试着尽可能多地说明代码

最新更新