[英]MVC View with Load and Form model
我有一个像表单一样工作的视图,因此它从ActionResult Index
中的控制器返回的 FormViewModel class 加载一堆下拉列表和 select。 将所有数据输入表单后,我使用Html.BeginForm("Create"
调用保存方法。ActionResult ActionResult Create(RequestForm model)
。所以问题是我想使用两种模型,一种用于加载,一种用于保存数据。我现在正在做的是从 FormViewModel 和 RequestForm model 中获取 object。
有一个更好的方法吗?
看法:
@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; }
申请表 Model
public class RequestForm
{
[Required]
public int? CountryId { get; set; }
}
控制器
public ActionResult Create(RequestForm model)
{
var FormInfo = FormCreate(model);
return View("");
}
您可以嵌套视图模型,并且只在表单发布时提交您想要的模型。
例如:
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]
放在要向其发布数据的视图 model 上,因为这是在验证用户数据。 您不需要将它放在CountryViewModel
上,因为您正在自己填充列表。
最后,还有一件重要的事情需要注意,那就是表单发送到的方法的参数名称:
public ActionResult Create(CreateRequestViewModel data)
{
...
}
参数的名称必须与您在外部 model, FormViewModel
中声明的名称相匹配。
您可以使用 2 个模型(一个加载表单内容,另一个发送选择,输入到服务器)。 Model 绑定为您完成工作,即当它到达 Controller 的 Action 方法( Create )时,它将输入值转换为 Model class( RequestForm ) class
根据您分享的信息,这种方法似乎可以
您可以使用一个视图 model 在视图中加载数据并使用它将数据发布到服务器,然后在服务器上您可以将 model 转换为将保存到数据库的新实体。
@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; }
Controller
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) ;
}
我试图尽可能多地说明代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.