我有一个razor / mvc3应用程序。我试图在一个视图上放置2个表单,并且在两个表单上都存在数据问题。我添加了一个扩展方法,通过在字段'id中添加前缀来更改templateinfo。不幸的是,来自输入的数据现在根本没有在帖子中持续存在,我不确定还需要添加什么才能使数据保持不变。这是:
public static class ExtensionMethods
{
public static IDisposable BeginHtmlFieldPrefixScope(this HtmlHelper html, string htmlFieldPrefix)
{
return new HtmlFieldPrefixScope(html.ViewData.TemplateInfo, htmlFieldPrefix);
}
//this is an extension helper class for input id prefixes for the BeginHtmlFieldPrefixScope extension method
private class HtmlFieldPrefixScope : IDisposable
{
private TemplateInfo TemplateInfo;
private string previousHtmlFieldPrefix;
public HtmlFieldPrefixScope(TemplateInfo templateInfo, string htmlFieldPrefix)
{
TemplateInfo = templateInfo;
previousHtmlFieldPrefix = templateInfo.HtmlFieldPrefix;
templateInfo.HtmlFieldPrefix = htmlFieldPrefix;
}
public void Dispose()
{
TemplateInfo.HtmlFieldPrefix = previousHtmlFieldPrefix;
}
}
我正在使用这种方法:
@using (Html.BeginForm("LogIn","Account"))
{
using (Html.BeginHtmlFieldPrefixScope("LoginForm"))
{
<div>
<fieldset>
<legend>Account Information</legend>
<div class="editor-label">
@Html.LabelFor(m => m.UserName)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.Password)
</div>
<div class="editor-field">
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
</div>
<div class="editor-label">
@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe)
</div>
<p>
<input type="submit" value="Log On" />
</p>
</fieldset>
</div>
}
}
更改输入字段ID的前缀。 谢谢你的帮助
答案 0 :(得分:0)
ViewData不会在帖子中保留。您需要TempDate(对下一个请求有效)或Session(或Cache)