Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 清除post方法中的文本区域_C#_Asp.net Mvc_Razor - Fatal编程技术网

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();在返回之前…当您返回模型时,列表对象看起来是空的。