我需要从局部视图中获取所选的下拉值并包含在Book类的CategoryID中...
[Authorize]
public PartialViewResult GetAllCategory()
{
ProcessSVC.Category newCategory = new ProcessSVC.Category();
newCategory.ChildCategories = obj1.GetAllCategories(String.Empty);
return PartialView(newCategory);
}
GetAllCategory.cshtml(PartialView)
@model MvcAdminTemplate.ProcessSVC.Category
@Html.DropDownListFor(m => m.ParentID, new SelectList(Model.ChildCategories, "ID", "DisplayName"), new { @class = "form-control" })
创建视图:
[Authorize]
[HttpPost]
public ActionResult Create()
{
MvcAdminTemplate.ProcessSVC.Book oBook = new ProcessSVC.Book();
return View(oBook);
}
Create.cshtml
@model MvcAdminTemplate.ProcessSVC.Book
@{
ViewBag.Title = "Create";
}
@Html.Partial("_LeftMenu")
<!-- content -->
<h2>Create</h2>
<div class="col-md-10">
@using (Html.BeginForm("Create", "Books", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="row">
<div class="bootstrap-admin-no-table-panel-content bootstrap-admin-panel-content collapse in">
<form class="form-horizontal">
<fieldset>
<legend>Add Book</legend>
<div class="form-group">
<div class="col-lg-2">
@Html.LabelFor(m => m.BookName, new { @class = "control-label" })
</div>
<div class="col-lg-10">
@Html.TextBoxFor(m => m.BookName, new { @class = "form-control", type = "text" })
<p class="help-block"> </p>
</div>
</div>
<div class="form-group">
<div class="col-lg-2">
@Html.LabelFor(m => m.Description, new { @class = " control-label" })
</div>
<div class="col-lg-10">
@Html.TextBoxFor(m => m.Description, new { @class = "form-control ", type = "text" })
<p class="help-block"> </p>
</div>
</div>
<div class="form-group divCategory">
<div class="col-lg-2">
@Html.Label("Parent Category", new { @class = " control-label" })
</div>
<div class="col-lg-10">
@Html.HiddenFor(m => m.BookId, new { @class = "hdn-id" })
@Html.Action("GetAllCategory","Books")
<span class="help-block"> </span>
</div>
<button type="submit" class="btn btn-primary">Save changes</button>
<button type="reset" class="btn btn-default">Cancel</button>
</div>
</fieldset>
</form>
</div>
</div>
}
提交上述表格后,我需要获取Book属性和类别“Selected Category”。
[Authorize]
[HttpPost]
public ActionResult Create(ProcessSVC.Book oBook)
{
oBook.LanguageID = 1;
_service.AddBooks(oBook.ActualPrice,oBook.ActualPriceString, oBook.Author, oBook.BookId, oBook.BookName, oBook.CategoryID, oBook.Currency, oBook.CurrentPrice, oBook.CurrentPriceString,
oBook.Description, oBook.DiscountPercentage, oBook.DiscountValue, oBook.LanguageID,
oBook.NativeLanguageName, oBook.Publisher);
TempData.Add("SuccessMessage", " New book " + oBook.BookName + " Added !");
return RedirectToAction("Index");
}
请建议我。
答案 0 :(得分:2)
要正确绑定模型,您需要在主视图中创建下拉列表。您在局部视图中的这一行
@Html.DropDownListFor(m => m.ParentID, ....
将呈现选择
<select name="ParentID" ...
但是你的模型期待一个名为CategoryID
在Create()
方法中,生成SelectList
并分配给视图模型属性或分配到ViewBag
然后(代替或@Html.Action("GetAllCategory","Books")
使用
@Html.DropDownListFor(m => m.CategoryID, ....
答案 1 :(得分:0)
在GetAllCategory.cshtml文件中使用此行
@Html.DropDownList("CategoryID", new SelectList(Model.ChildCategories, "ID", "DisplayName"), new { @class = "form-control" })