简体   繁体   English

MVC3 HTML助手不会在提交表单时更新DropdownListFor

[英]MVC3 HTML helper doesn't update DropdownListFor on Submit the form

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. 我有一个简单的HTML表单,其中dropdwonListFor绑定到颜色,在它下面有一个textBox并提交按钮以提交表单并保存颜色。

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. 它返回到控制器,我保存了texebox中的颜色​​并作为操作结果返回view(model),但是问题是dropdownlistfor不会随文本框的值更新,无论文本框内文本框中的值是否是否下拉列表。

By the way you can test it urself Can anybody help please ? 顺便说一下,您可以自己测试一下。有人可以帮忙吗?

Model.cs 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" />

} }

Model 模型

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

Controller 调节器

public ActionResult Index() {
        var model = new TestModel();
        model.Color="Blue";
        var colors =new List<SelectListItem>() { new SelectListItem() { Text =  "Blue", Value = "Blue" }, new SelectListItem() { Text = "Red", Value = "red" } };
        model.Colors = new SelectList(colors,"Text","Value");

        return View(model);
    }

[HttpPost] public ActionResult Index(TestModel model) {
        model.Color="Red";

        var colors =new List<SelectListItem>() { new SelectListItem() { Text =  "Blue", Value = "Blue" }, new SelectListItem() { Text = "Red", Value = "red" } };
        model.Colors = new SelectList(colors,"Text","Value");

        return View(model); }

View 视图

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

You need to include all colors in your Post action. 您需要在“发布”操作中包括所有颜色。

Also do not use ViewData but add the items to you view model: 也不要使用ViewData,而是将项目添加到您的视图模型中:

public class TestModel {
    public String Color { get; set; }
    IEnumerable<SelectListItem> AvailableColors {get;set;}
}

But since view models are intended to be used to abstract away your models you could do something like: 但是由于视图模型旨在用于抽象化模型,因此您可以执行以下操作:

public class TestModel {
    public TestModel(IEnumerable<string> colors)
    {
        AvailableColors = colors.Select(c => new SelectListItem{Text=c, Value = c});
    }

    public String Color { get; set; }
    IEnumerable<SelectListItem> AvailableColors {get;}
}

And in your controller: 在您的控制器中:

public ActionResult Index() {
    var model = new TestModel(new string[]{"Red", "Green", "Blue"});
    model.Color="Blue";
    return View(model);
}

In order to make the dropdown list change what is selected you have to set the Selected attribute of the select list item corresponding to the textbox value to true. 为了使下拉列表更改所选内容,您必须将与文本框值对应的选择列表项的Selected属性设置为true。 Something like this: (Note: I did not compile and test this. Tweaks may be needed to get it to compile. Also, I assumed that if a color was not in the list it should be added.) 这样的事情:(注意:我没有进行编译和测试。可能需要进行一些调整才能使其进行编译。此外,我假设如果颜色不在列表中,则应该添加它。)

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

        SelectListItem selectedColor = colors.Where(c => c.Text == model.Color).FirstOrDefault();
        if (selectedColor != null)
        {
            selectedColor.Selected = true;
        }
        else
        {
            colors.Add(new SelectListItem() { Text = model.Color; Value = model.Color; Selected = true; };
        }
        ViewData["Colors"] = colors;
        return View(model);
}

EDIT 编辑

After doing some testing and digging, it appears that you will need to use javascript to do this as explained in this SO question . 经过一些测试和挖掘之后,看来您将需要使用javascript来执行此操作,如本SO问题中所述 Another option is to roll your own helper. 另一种选择是使用自己的助手。

Okay guys, the problem is not about the way you implement this scenario, the problem here is ModelState. 好的,伙计们,问题不在于实现此方案的方式,而是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. 第二次渲染视图时,它将查看ModelState并使用这些值填充控件。 So simply we need to clear the ModelState before returning the View. 因此,我们只需要在返回View之前清除ModelState。

Model.cs 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 啦啦队

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM