C# 清除post方法中的文本区域
我有一张表格。在该表单中,我有一个文本区域属性。单击“提交”按钮后,我希望在清除表单时在同一页面中显示一些视图包值 在我的场景中,除了一个文本区域,所有内容都变得清晰。因此 我使用了以下方法 模型类C# 清除post方法中的文本区域,c#,asp.net-mvc,razor,C#,Asp.net Mvc,Razor,我有一张表格。在该表单中,我有一个文本区域属性。单击“提交”按钮后,我希望在清除表单时在同一页面中显示一些视图包值 在我的场景中,除了一个文本区域,所有内容都变得清晰。因此 我使用了以下方法 模型类 public class SomeModel { ... public list<user> userlist {get; set} } [HttpPost] public ActionResult SomeAction(SomeModel model) { mod
public class SomeModel
{
...
public list<user> userlist {get; set}
}
[HttpPost]
public ActionResult SomeAction(SomeModel model)
{
model.userlist = new List<user>();
if (ModelState.IsValid)
{
.....
ModelState.Clear();
ModelState.Remove(model.SampleTextArea);
model.SampleTextArea = ""
}
return View("SomeAction", model);
}
公共类模型
{
...
公共列表用户列表{get;set}
}
控制器类
public class SomeModel
{
...
public list<user> userlist {get; set}
}
[HttpPost]
public ActionResult SomeAction(SomeModel model)
{
model.userlist = new List<user>();
if (ModelState.IsValid)
{
.....
ModelState.Clear();
ModelState.Remove(model.SampleTextArea);
model.SampleTextArea = ""
}
return View("SomeAction", model);
}
[HttpPost]
公共行动结果SomeAction(SomeModel模型)
{
model.userlist=新列表();
if(ModelState.IsValid)
{
.....
ModelState.Clear();
ModelState.Remove(model.SampleTextArea);
model.SampleTextArea=“”
}
返回视图(“SomeAction”,模型);
}
现在一切第一次都很好。但如果我在此表单中填写相同的值并单击“提交”,则不会刷新页面
因为它说模型验证无效,所以从第二次尝试开始,对于相同的模型值,该函数不起作用
它说“userlist”System.Int[32]为空时出错
如何在没有页面定向的情况下正确清除此文本区域值(RedirecToAction())请参阅。我使用此代码从ModelState中删除单个条目:
/// <summary>
/// Removes the ModelState entry for this property.
/// </summary>
public static void RemoveFor<M, P>(this ModelStateDictionary modelState,
Expression<Func<M, P>> property)
{
string key = KeyFor(property);
modelState.Remove(key);
}
/// <summary>
/// Returns the ModelState key used for this property.
/// </summary>
private static string KeyFor<M, P>(Expression<Func<M, P>> property)
{
return ExpressionHelper.GetExpressionText(property);
}
SomeModel的默认构造函数是否实例化了一个新列表?是的,这在第一次尝试和第二次尝试之后都有效,对于相同的模型属性,这不起作用。您是否尝试过设置model.userlist=new List();在返回之前…当您返回模型时,列表对象看起来是空的。