MVC3 HTML帮助不更新DropdownListFor上提交表单 [英] MVC3 HTML helper doesn't update DropdownListFor on Submit the form

查看:134
本文介绍了MVC3 HTML帮助不更新DropdownListFor上提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有dropdwonListFor一个简单的HTML表单绑定到颜色,它下面的一个文本框和提交按钮提交表单和保存颜色。

I have a simple HTML form with dropdwonListFor bound to colors, a textBox below it and submit button to submit the form and save the color.

当我选择DropDownList的颜色,它会改变它下面的文本框的值,如果用户点击提交表单。它可以追溯到控制器和I保存从texebox和返回视图(模型)作为操作结果的颜色,但该dropdownlistfor不会与文本框的值更新问题是否在内部的文本框中的值DROPDOWNLIST与否。

When I select a color from the dropdownlist, it will change the value of the textbox below it, if the user clicks the submit form. it goes back to the controller and I save the color from the texebox and return view(model) as an action result, but the problem that the dropdownlistfor doesn't get updated with the value of the textbox whether the value in the textbox within the dropdownlist or not.

顺便说一句,你可以测试它urself
任何人可以帮助吗?

By the way you can test it urself Can anybody help please ?

Model.cs

public class TestModel {
    public String Color { get; set; }
}

Controller.cs

Controller.cs

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);
}

Index.cs

Index.cs

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

}

推荐答案

好球员,问题是不是你实现这个场景的方式,这里的问题是ModelState中。我张贴的行动,回到了同样的观点。该视图显示它的第二次会看的ModelState和使用这些值来填充的控件。
所以,简单地,我们需要返回视图之前清除的ModelState。

Okay guys, the problem is not about the way you implement this scenario, the problem here is ModelState. I POST to the Action and return the same view. The second time the view is rendered it will look at the ModelState and use those values to fill the controls. So simply we need to clear the ModelState before returning the View.

Model.cs

public class TestModel {
    public String Color { get; set; }
}

Controller.cs

Controller.cs

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);
}

Index.cs

Index.cs

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

}

Cheeeeeers

Cheeeeeers

这篇关于MVC3 HTML帮助不更新DropdownListFor上提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆