C# 这里的问题是ModelState。我发布到动作并返回相同的视图。第二次呈现视图时,它将查看ModelState并使用这些值填充控件。 因此,我们只需在返回视图之前清除ModelState

C# 这里的问题是ModelState。我发布到动作并返回相同的视图。第二次呈现视图时,它将查看ModelState并使用这些值填充控件。 因此,我们只需在返回视图之前清除ModelState,c#,asp.net-mvc,asp.net-mvc-3,asp.net-mvc-2,html-helper,C#,Asp.net Mvc,Asp.net Mvc 3,Asp.net Mvc 2,Html Helper,Model.cs public class TestModel { public String Color { get; set; } } public class TestModel { public String Color { get; set; } } 控制器 public ActionResult Index() { var model = new TestModel(); model.Color="Blue"; Vi

Model.cs

public class TestModel {
    public String Color { get; set; }
}
public class TestModel {
    public String Color { get; set; }
}
控制器

public ActionResult Index() {
        var model = new TestModel();
        model.Color="Blue";
        ViewData["Colors"]=new List<SelectListItem>() { new SelectListItem() { Text =  "Blue", Value = "Blue" }, new SelectListItem() { Text = "Red", Value = "Red" } };
        return View(model);
    }

[HttpPost]
public ActionResult Index(TestModel model) {
        model.Color="Red";
        ViewData["Colors"]=new List<SelectListItem>() { new SelectListItem() { Text =  "Blue", Value = "Blue" }, new SelectListItem() { Text = "Red", Value = "Red" } };
        return View(model);
}
public ActionResult Index() {
        var model = new TestModel();
        model.Color="Blue";
        ViewData["Colors"]=new List<SelectListItem>() { new SelectListItem() { Text =  "Blue", Value = "Blue" }, new SelectListItem() { Text = "Red", Value = "Red" } };


        return View(model);
    }

[HttpPost]
public ActionResult Index(TestModel model) {
        model.Color="Red";
        ViewData["Colors"]=new List<SelectListItem>() { new SelectListItem() { Text =  "Blue", Value = "Blue" }, new SelectListItem() { Text = "Red", Value = "Red" } };

        ***ModelState.Clear();***
        return View(model);
}
public ActionResult Index(){
var model=newtestmodel();
model.Color=“蓝色”;
ViewData[“Colors”]=new List(){new SelectListItem(){Text=“Blue”,Value=“Blue”},new SelectListItem(){Text=“Red”,Value=“Red”};
返回视图(模型);
}
[HttpPost]
公共行动结果索引(TestModel){
model.Color=“红色”;
ViewData[“Colors”]=new List(){new SelectListItem(){Text=“Blue”,Value=“Blue”},new SelectListItem(){Text=“Red”,Value=“Red”};
***ModelState.Clear()***
返回视图(模型);
}
Index.cs

@using (Html.BeginForm()) {
@Html.DropDownListFor(m => m.Color, ViewData["Colors"], new { @class = "w200" })
<input type="submit" />
@using (Html.BeginForm()) {
@Html.DropDownListFor(m => m.Color, ViewData["Colors"], new { @class = "w200" })
<input type="submit" />
@使用(Html.BeginForm()){
@DropDownListFor(m=>m.Color,ViewData[“Colors”],new{@class=“w200”})
}


Cheeeeers

好了,伙计们,问题不在于实现这个场景的方式,这里的问题是ModelState。我发布到动作并返回相同的视图。第二次呈现视图时,它将查看ModelState并使用这些值填充控件。 因此,我们只需在返回视图之前清除ModelState

Model.cs

public class TestModel {
    public String Color { get; set; }
}
public class TestModel {
    public String Color { get; set; }
}
控制器

public ActionResult Index() {
        var model = new TestModel();
        model.Color="Blue";
        ViewData["Colors"]=new List<SelectListItem>() { new SelectListItem() { Text =  "Blue", Value = "Blue" }, new SelectListItem() { Text = "Red", Value = "Red" } };
        return View(model);
    }

[HttpPost]
public ActionResult Index(TestModel model) {
        model.Color="Red";
        ViewData["Colors"]=new List<SelectListItem>() { new SelectListItem() { Text =  "Blue", Value = "Blue" }, new SelectListItem() { Text = "Red", Value = "Red" } };
        return View(model);
}
public ActionResult Index() {
        var model = new TestModel();
        model.Color="Blue";
        ViewData["Colors"]=new List<SelectListItem>() { new SelectListItem() { Text =  "Blue", Value = "Blue" }, new SelectListItem() { Text = "Red", Value = "Red" } };


        return View(model);
    }

[HttpPost]
public ActionResult Index(TestModel model) {
        model.Color="Red";
        ViewData["Colors"]=new List<SelectListItem>() { new SelectListItem() { Text =  "Blue", Value = "Blue" }, new SelectListItem() { Text = "Red", Value = "Red" } };

        ***ModelState.Clear();***
        return View(model);
}
public ActionResult Index(){
var model=newtestmodel();
model.Color=“蓝色”;
ViewData[“Colors”]=new List(){new SelectListItem(){Text=“Blue”,Value=“Blue”},new SelectListItem(){Text=“Red”,Value=“Red”};
返回视图(模型);
}
[HttpPost]
公共行动结果索引(TestModel){
model.Color=“红色”;
ViewData[“Colors”]=new List(){new SelectListItem(){Text=“Blue”,Value=“Blue”},new SelectListItem(){Text=“Red”,Value=“Red”};
***ModelState.Clear()***
返回视图(模型);
}
Index.cs

@using (Html.BeginForm()) {
@Html.DropDownListFor(m => m.Color, ViewData["Colors"], new { @class = "w200" })
<input type="submit" />
@using (Html.BeginForm()) {
@Html.DropDownListFor(m => m.Color, ViewData["Colors"], new { @class = "w200" })
<input type="submit" />
@使用(Html.BeginForm()){
@DropDownListFor(m=>m.Color,ViewData[“Colors”],new{@class=“w200”})
}



Cheeeeers

我们如何在看不到您的代码的情况下自己测试它?创建一个MVC项目,放置一个dropdownlist for和TextBoxFor以及一个submit按钮,在绑定到dropdownlist的controller change属性中,您将看到它保留了旧值而不是更新的值,在您的问题中添加代码不是更好吗?我们免费为您提供帮助,这意味着当您可以改进您的问题时,我们大多数人都不愿意重复您的错误。给您,我感谢您的帮助!也许我遗漏了什么,但我在你的表格中没有看到文本框。它在哪里?我们如何在看不到您的代码的情况下自己测试它?创建一个MVC项目,放置一个dropdownlist for和TextBoxFor以及一个submit按钮,在绑定到dropdownlist的controller change属性中,您将看到它保留了旧值而不是更新的值,在您的问题中添加代码不是更好吗?我们免费为您提供帮助,这意味着当您可以改进您的问题时,我们大多数人都不愿意重复您的错误。给您,我感谢您的帮助!也许我遗漏了什么,但我在你的表格中没有看到文本框。它在哪里?这是同样的事情,我可以装饰它,做同样的事情,但这不是我的问题,问题是HTML助手没有更新dropdownList,请在你的浏览器上测试它machine@Alaa奥斯塔:你测试过我的变化吗?这和你的代码不一样,我只是用你发布的代码测试了一下,颜色没有从蓝色变为红色。请你自己检查一下,跟我回去这对我来说太重要了us@Alaa奥斯塔:现在,试着换个型号;按型号。Color=“red”;Nada先生,我已经试过了,模型会有更新的值,但dropdownListFor在提交formIt时不会得到更新是一样的,我可以装饰它并做同样的事情,但这不是我的问题,问题是HTML助手没有更新dropdownList,请在你的电脑上测试一下machine@Alaa奥斯塔:你测试过我的变化吗?这和你的代码不一样,我只是用你发布的代码测试了一下,颜色没有从蓝色变为红色。请你自己检查一下,跟我回去这对我来说太重要了us@Alaa奥斯塔:现在,试着换个型号;按型号。Color=“red”;Nada先生,我已经试过了,模型会有更新的值,但是dropdownListFor在提交表单时不会更新,包括在内,请测试它urselfNo,它也不起作用,请测试它urself,我想知道我是否做错了什么或者我遗漏了什么,谢谢。包括在内,请测试它urselfNo,它也不起作用,请自己测试,我想知道我是做错了什么还是遗漏了什么,谢谢。很抱歉,它也不起作用,请自己测试确认。谢谢你的邀请effort@AlaaOsta-我很惊讶它不起作用。但是我确实找到了一个答案(请参见我的编辑)。你需要清除ModalState的答案,因为它仍然具有旧的视图检查我的答案很抱歉,它也不起作用,请自己测试以确认这一点。谢谢你的邀请effort@AlaaOsta-我很惊讶它不起作用。但是我确实找到了一个答案(见我的编辑)。答案是你需要清除ModalState,因为它仍然有旧的视图检查我的答案
@using (Html.BeginForm()) {
@Html.DropDownListFor(m => m.Color, ViewData["Colors"], new { @class = "w200" })
<input type="submit" />