C# 在ASP.NET MVC3中更新部分渲染视图
我有一个模型,它有一些特性:C# 在ASP.NET MVC3中更新部分渲染视图,c#,asp.net-mvc,asp.net-mvc-3,C#,Asp.net Mvc,Asp.net Mvc 3,我有一个模型,它有一些特性: public class AddComponentsDialogModel { public string NameStartLabel { get; set; } [Required] public string NameStart { get; set; } public string NameEndLabel { get; set; } public string NameEnd { get; set; }
public class AddComponentsDialogModel
{
public string NameStartLabel { get; set; }
[Required]
public string NameStart { get; set; }
public string NameEndLabel { get; set; }
public string NameEnd { get; set; }
public List<AttributeModel> Attributes { get; set; }
public AddComponentsDialogModel()
{
NameStartLabel = "Name Start";
NameEndLabel = "Name End";
Attributes = new List<AttributeModel>();
}
}
公共类AddComponentsDialogModel
{
公共字符串NameStartLabel{get;set;}
[必需]
公共字符串名称启动程序{get;set;}
公共字符串NameEndLabel{get;set;}
公共字符串NameEnd{get;set;}
公共列表属性{get;set;}
public addComponentDialogModel()
{
NameStartLabel=“Name Start”;
NameEndLabel=“Name End”;
属性=新列表();
}
}
以及相应的观点:
<% Html.BeginForm(); %>
<div class="detailsRow required">
<%= Html.LabelFor(model => model.NameStart, Model.NameStartLabel)%>
<%= Html.EditorFor(model => model.NameStart) %>
</div>
<div class="detailsRow">
<%= Html.LabelFor(model => model.NameEnd, Model.NameEndLabel)%>
<%= Html.EditorFor(model => model.NameEnd)%>
</div>
<div class="attributesArea">
<%= Html.EditorFor(model => model.Attributes) %>
</div>
<% Html.EndForm(); %>
model.NameStart,model.NameStart标签)%%>
model.NameStart)%%>
model.NameEnd,model.NameEndLabel)%%>
model.NameEnd)%%>
模型属性)%%>
第一次加载时,“属性”列表为空。因此,不会渲染属性视图
然后,我想从客户端发出一个ajax请求,并更新attributesArea:
var attributeIDs = [1, 2, 3];
$('div.attributesArea').load('../Attribute/GetAttributeViews', $.param({
attributeIDs: attributeIDs
}, true));
[HttpGet, AjaxOnly]
public ActionResult GetAttributeViews(IEnumerable<int> attributeIDs)
{
List<CSAttribute> attributes = new List<CSAttribute>();
if (attributeIDs != null)
{
attributes.AddRange(AttributeCache.Instance.GetByIDList(attributeIDs));
}
List<AttributeModel> attributeModels = new List<AttributeModel>(attributes.Select(a => new AttributeModel(a)));
return PartialView("EditorTemplates/AttributeModels", attributeModels);
}
var attributeIDs=[1,2,3];
$('div.attributesArea').load('../Attribute/GetAttributeViews',$.param({
attributeIDs:attributeIDs
},对);
[HttpGet,AjaxOnly]
公共操作结果GetAttributeViews(IEnumerable AttributeId)
{
列表属性=新列表();
if(attributeId!=null)
{
attributes.AddRange(AttributeCache.Instance.GetByIDList(AttributeId));
}
List attributeModels=新列表(attributes.Select(a=>newattributemodel(a));
返回PartialView(“EditorTemplates/AttributeModels”,AttributeModels);
}
虽然加载的HTML在视觉上是正确的,但它缺少适当的HTML标记,使MVC模型绑定器能够理解它
如果我将表单发回控制器,则表明有0个属性,但应该有一些属性,因为我刚刚刷新了HTML
做这件事的正确方法是什么?必须重新加载整个AddComponentDialog的视图似乎是不正确的,因为这样一来,渲染子视图的意义何在
以下是属性的HTML标记在最初呈现时的外观:
<div class="attributesArea">
<input data-val="true" data-val-number="The field ID must be a number." data-val-required="The ID field is required." id="Attributes_0__ID" name="Attributes[0].ID" type="hidden" value="67">
<input id="Attributes_0__Name" name="Attributes[0].Name" type="hidden" value="Memory">
<input data-val="true" data-val-required="The DataType field is required." id="Attributes_0__DataType" name="Attributes[0].DataType" type="hidden" value="AlphaNumeric">
<input data-val="true" data-val-number="The field DataLength must be a number." data-val-required="The DataLength field is required." id="Attributes_0__DataLength" name="Attributes[0].DataLength" type="hidden" value="20">
<input class="component-alphanumeric component-editable" id="Attributes_0__Value" maxlength="20" name="Attributes[0].Value" type="text" value="">
</div>
如果我从GetAttributeViews加载它:
<div class="attributesArea">
<input name="[0].ID" type="hidden" value="67">
<input name="[0].Name" type="hidden" value="Memory">
<input name="[0].DataType" type="hidden" value="AlphaNumeric">
<input name="[0].DataLength" type="hidden" value="20">
<input class="component-alphanumeric component-editable" maxlength="20" name="[0].Value" type="text" value="">
</div>
检查此项