我将部分视图呈现为Ajax请求的一部分.
当我从视图中调用局部视图时:
int i=0;
foreach(var rule in Model.Rules) {
@Html.Partial("ValidationRuleRow", rule, new ViewDataDictionary {
TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = string.Format("Rules[{0}]", i) } })
i++;
}
Run Code Online (Sandbox Code Playgroud)
我能够设置HtmlFieldPrefix以允许正确的模型绑定.
我希望用户能够通过ajax动态添加新的ValidationRuleRow,如:
$.ajax({
type: "GET",
url: "/Monitors/NewMonitorValidationRule",
success: function (data, textStatus, jqXHR) {
var element = $(data);
$("#ValidationRuleContainer").append(element);
}
});
Run Code Online (Sandbox Code Playgroud)
所以我在我的控制器中有一个动作来获取HTML:
public ActionResult NewMonitorValidationRule()
{
ValidationRule rule = new ValidationRule{Id = TempSurrogateKey.Next};
var view = PartialView("ValidationRuleRow", rule);
// CODE TO SET PartialView field prefix
return view;
}
Run Code Online (Sandbox Code Playgroud)
返回的HTML没有前缀.无论如何在从Controller中的Action调用PartialView时设置前缀?
我有一个ASPNET MVC 2项目.我用的时候
<%= Html.TextBoxFor(model => model.Login) %>
Run Code Online (Sandbox Code Playgroud)
TexBoxFor将呈现为
<input id="Login" name="Login" type="text" value="" />
Run Code Online (Sandbox Code Playgroud)
模型中的字段是
[Required(ErrorMessage = "")]
[DisplayName("Login")]
public string Login { get; set; }
Run Code Online (Sandbox Code Playgroud)
我可以用一些前缀创建id和name属性吗?喜欢
<input id="prefixLogin" name="prefixLogin" type="text" value="" />
Run Code Online (Sandbox Code Playgroud)
谢谢大家.
问题是:当我在页面上放置2个相同类型的控件时,我需要为绑定指定不同的前缀.在这种情况下,在表单不正确之后生成的验证规则.那么如何让客户端验证工作呢?
该页面包含:
<%
Html.RenderPartial(ViewLocations.Shared.PhoneEditPartial, new PhoneViewModel { Phone = person.PhonePhone, Prefix = "PhonePhone" });
Html.RenderPartial(ViewLocations.Shared.PhoneEditPartial, new PhoneViewModel { Phone = person.FaxPhone, Prefix = "FaxPhone" });
%>
Run Code Online (Sandbox Code Playgroud)
控件ViewUserControl <PhoneViewModel>:
<%= Html.TextBox(Model.GetPrefixed("CountryCode"), Model.Phone.CountryCode) %>
<%= Html.ValidationMessage("Phone.CountryCode", new { id = Model.GetPrefixed("CountryCode"), name = Model.GetPrefixed("CountryCode") })%>
Run Code Online (Sandbox Code Playgroud)
其中Model.GetPrefixed("CountryCode")只返回"FaxPhone.CountryCode"或"PhonePhone.CountryCode"取决于前缀
这是表单后生成的验证规则.它们被复制为字段名"Phone.CountryCode".虽然期望的结果是每个FieldNames"FaxPhone.CountryCode","PhonePhone.CountryCode" alt文本的 2个规则(必需,数量),但是 http://www.freeimagehosting.net/uploads/37fbe720bf.png
这个问题与Asp.Net MVC2客户端验证和重复ID的问题有些重复, 但建议手动生成ID并没有帮助.