我几乎在试图使用默认的asp.net MVC功能来完成这项任务。首先,我将介绍我到目前为止的内容以及我的目标是什么。
我有这两个型号。
public class ClassifiedViewModel
{
public int? ClassifiedId { get; set; }
public List<ClassifiedWishListViewModel> wishListModel {get; set;}
[Required]
public String UserId { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Description { get; set; }
}
public class ClassifiedWishListViewModel
{
public string KeyWord { get; set; }
public int CategoryId { get; set; }
public int SubCategoryId { get; set; }
}
添加了更多信息
查看
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.ClassifiedId, new { htmlAttributes = new { @class = "form-control" } })
@Html.HiddenFor(model => model.UserId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="form-group">
@Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-3">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextAreaFor(model => model.Description, new { @class = "form-control", rows = "10", cols = "60" })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group" id="wishList">
</div>
<button type="button" class="btn" id="btnAddWish">
<span>Add item</span>
</button>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<button type="submit" class="btn btn-default" id="btnSave">Save</button>
</div>
</div>
</div>
}
脚本
$("#btnAddWish").on("click", function () {
$.get('/Classified/ClassifiedWishList', function (template) {
$("#wishList").append(template);
});
});
控制器 - 获取部分视图的方法
[HttpGet]
public ActionResult ClassifiedWishList()
{
var model = new ClassifiedWishListViewModel();
var categoriesBO = new CategoriesBO();
var categories = categoriesBO.GetAllCategories();
model.Categories =
from c in categories
select new SelectListItem
{
Text = c.Name,
Value = c.CategoryId.ToString()
};
return PartialView("ClassifiedWishList", model);
}
我正在尝试创建一个视图来编辑分类。但classifiedwishList是一个变量长度属性。所以,我创建了一个局部视图,以相同的形式多次生成ClassigiedWishListViewModel编辑器。但是当我尝试提交整个表单时,ClassifiedWishListViewModel永远不会绑定到我原来的ClassifiedViewModel属性。
有没有一种方法可以通过简单的使用Asp.net MVC功能来实现?我看的每个地方都建议我使用javascript框架并通过序列化的jsons将所有信息发送到Controller,但是通过使用它,我将失去Annotations执行的所有验证(如required,maxlenght,其他......) MVC,对吗?
如果你在Asp.net MVC上需要一个更加动态的页面,你必须忘记框架为你提供的所有工具并开始通过JSON通信发送和检索数据,这真的令人沮丧。
欢迎任何形式的帮助!多谢你们!