将列表数据与 ViewModel 一起传递,以在下拉列表中查看和填充列表数据
Pass List data along with ViewModel to view and populate list data in drop down
我创建了 ModelView,目的是通过 view-- razor 表单创建新的记录实例。在控制器中,我需要将列表数据分配给 ViewModel; IEnumerable 然后将此模型与列表数据(类别类型)一起传递,然后在视图中我需要填充下拉列表,然后将所选类别类型的 ID 与其他数据一起发送回控制器。
我已将 IEnumerable 值分配给控制器中的模型,但不确定是否正确以及如何在视图中执行其余部分???
查看模型 - CompanyProfileModelView
public class CompanyProfileModelView
{
public Company _Company { get; set; }
public IEnumerable<CategoryType> _CategoryType { get; set; }
}
型号Class
public class CategoryType
{
public int CategoryTypeID { get; set; }
public string CategoryTitle { get; set; }
public ICollection<Company> Companies { get; set; }
}
控制器Class
[HttpGet]
public ActionResult CreateCompany()
{
var _listData = _appFunctions.GetAllCategory();
var _model = new CompanyProfileModelView
{
_CategoryType = _listData
??????????????
};
return PartialView("_CreateNewCompanyPartial", _model);
}
[HttpPost]
public ActionResult CreateCompany(CompanyProfileModelView _model)
{
try
{
if (ModelState.IsValid)
{
//my code will be here to read for input data
}
}
catch (DataException ex)
{
ModelState.AddModelError("", "Unable To Create New Function Navigation" + ex);
}
return RedirectToAction("Home");
}
查看
@model App.DAL.Model.CompanyProfileModelView
@using (Html.BeginForm("CreateCompany", "CompanyProfile", FormMethod.Post, new { id = "NewFunctionNavigationForm" }))
{
<div class="form-group">
@Html.LabelFor(@model => @model._Company.CompanyName, new { @class = "control-label col-md-2" })
<div class="form-group">
@Html.EditorFor(@model =>@model._Company.CompanyName)
@Html.ValidationMessageFor(@model=>@model._Company.CompanyName)
</div>
</div>
<div class="form-group">
// need help here for dropdown of CategoryType list
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
}
由于DropDownList
使用IEnumerable<SelectListItem>
,将视图模型更改为
public class CompanyProfileModelView
{
public Company Company { get; set; }
public SelectList CategoryList { get; set; }
}
并假设 Company
模型包含
[Display(Name="Category")]
public int? CategoryType { get; set; }
控制器
[HttpGet]
public ActionResult CreateCompany()
{
var listData = _appFunctions.GetAllCategory();
var model = new CompanyProfileModelView
{
CategoryList = new SelectList(listData, "CategoryTypeID ", "CategoryTitle")
};
return View(model);
}
[HttpPost]
public ActionResult CreateCompany(CompanyProfileModelView model)
{
if (!ModelState.IsValid)
{
// Re-assign select list if returning the view
var listData = _appFunctions.GetAllCategory();
model.CategoryList = new SelectList(listData, "CategoryTypeID ", "CategoryTitle");
return View(model)
}
// Save and redirect
}
查看
@model App.DAL.Model.CompanyProfileModelView
@using (Html.BeginForm()) // Note, no parameters required in this case
{
....
@Html.LabelFor(m => m.Company.CategoryType, new { @class = "control-label col-md-2" })
@Html.DropDownListFor(m => m.Company.CategoryType, Model.CategoryList, "--Please select--")
@Html.ValidationMessageFor(m => m.Company.CategoryType)
.....
<input type="submit" value="Create" class="btn btn-default" />
}
我创建了 ModelView,目的是通过 view-- razor 表单创建新的记录实例。在控制器中,我需要将列表数据分配给 ViewModel; IEnumerable 然后将此模型与列表数据(类别类型)一起传递,然后在视图中我需要填充下拉列表,然后将所选类别类型的 ID 与其他数据一起发送回控制器。
我已将 IEnumerable 值分配给控制器中的模型,但不确定是否正确以及如何在视图中执行其余部分???
查看模型 - CompanyProfileModelView
public class CompanyProfileModelView
{
public Company _Company { get; set; }
public IEnumerable<CategoryType> _CategoryType { get; set; }
}
型号Class
public class CategoryType
{
public int CategoryTypeID { get; set; }
public string CategoryTitle { get; set; }
public ICollection<Company> Companies { get; set; }
}
控制器Class
[HttpGet]
public ActionResult CreateCompany()
{
var _listData = _appFunctions.GetAllCategory();
var _model = new CompanyProfileModelView
{
_CategoryType = _listData
??????????????
};
return PartialView("_CreateNewCompanyPartial", _model);
}
[HttpPost]
public ActionResult CreateCompany(CompanyProfileModelView _model)
{
try
{
if (ModelState.IsValid)
{
//my code will be here to read for input data
}
}
catch (DataException ex)
{
ModelState.AddModelError("", "Unable To Create New Function Navigation" + ex);
}
return RedirectToAction("Home");
}
查看
@model App.DAL.Model.CompanyProfileModelView
@using (Html.BeginForm("CreateCompany", "CompanyProfile", FormMethod.Post, new { id = "NewFunctionNavigationForm" }))
{
<div class="form-group">
@Html.LabelFor(@model => @model._Company.CompanyName, new { @class = "control-label col-md-2" })
<div class="form-group">
@Html.EditorFor(@model =>@model._Company.CompanyName)
@Html.ValidationMessageFor(@model=>@model._Company.CompanyName)
</div>
</div>
<div class="form-group">
// need help here for dropdown of CategoryType list
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
}
由于DropDownList
使用IEnumerable<SelectListItem>
,将视图模型更改为
public class CompanyProfileModelView
{
public Company Company { get; set; }
public SelectList CategoryList { get; set; }
}
并假设 Company
模型包含
[Display(Name="Category")]
public int? CategoryType { get; set; }
控制器
[HttpGet]
public ActionResult CreateCompany()
{
var listData = _appFunctions.GetAllCategory();
var model = new CompanyProfileModelView
{
CategoryList = new SelectList(listData, "CategoryTypeID ", "CategoryTitle")
};
return View(model);
}
[HttpPost]
public ActionResult CreateCompany(CompanyProfileModelView model)
{
if (!ModelState.IsValid)
{
// Re-assign select list if returning the view
var listData = _appFunctions.GetAllCategory();
model.CategoryList = new SelectList(listData, "CategoryTypeID ", "CategoryTitle");
return View(model)
}
// Save and redirect
}
查看
@model App.DAL.Model.CompanyProfileModelView
@using (Html.BeginForm()) // Note, no parameters required in this case
{
....
@Html.LabelFor(m => m.Company.CategoryType, new { @class = "control-label col-md-2" })
@Html.DropDownListFor(m => m.Company.CategoryType, Model.CategoryList, "--Please select--")
@Html.ValidationMessageFor(m => m.Company.CategoryType)
.....
<input type="submit" value="Create" class="btn btn-default" />
}