DropDownListFor并不总是显示选定值 [英] DropDownListFor not always showing selected value

查看:171
本文介绍了DropDownListFor并不总是显示选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作有使用餐酒codeR获取汽车信息的选项的应用程序。您可以在VIN输入,也可以通过下拉菜单,制造商,年份,品牌,型号等。

I'm working on an application that has the option of using a VIN decoder to get car information. You can type in the VIN, or you can select through dropdowns, manufacturer, year, make, model, etc.

制造商下拉列表是初始化页面上,没有选择的唯一有价值的事情。选择的制造商将找到该制造商的所有可用年,并返回的岁的列表中,以及返回制造商的列表,并选择所述一个。选择那么一年将通过应用程序返回可用的厂家,多年品牌的名单,与选定的制造商和选定今年无论鉴定,等下去。

The manufacturer dropdown is the only thing initialized on the page, with no selected value. Selecting a manufacturer will find all available years for that manufacturer and return the list of years, as well as returning the list of manufacturers and the one that was selected. Selecting a year will then return list of available manufacturers, years and makes, with the selected manufacturer and selected year both identified, and so on down through the application.

这个工作流程工作正常,我所有的下拉菜单中正确显示。虽然时输入VIN,我选择值为每个,并从中找到可用选项的列表,并准确渲染页面,我想如果有人用手工选择的选项到这一点。所有下拉菜单中使用正确的选择的属性正确地呈现我做到这一点,除了制造商时。

This workflow works fine and all of my dropdowns display correctly. When entering a VIN though, I have selected values for each, and still find the lists of available options, and render the page exactly as I would if someone had selected options by hand up to that point. All of the dropdowns render correctly with the proper selected attributes when I do this except the manufacturer.

我试图孤立尽可能和其他已经剥离出来的一切,我有现在这样:

I have tried to isolate it as much as possible, and have stripped out everything else, and I have this now:

查看:

@model My_Project.Models.Data 

@using System.Web.Helpers

@using (Html.BeginForm("Temp", "Home", FormMethod.Post, new { id = "formIndex" }))
{
    <div>
        VIN:&nbsp;
        @Html.TextBox("vin", Model.VIN) <button type="submit">Go!</button>
    </div>
    <div>
        Manufacturer: (@Model.ManufacturerId)
        @Html.DropDownListFor(m => m.ManufacturerId, Model.Manufacturers, new { style = "width: 175px;" })
    </div>
}

型号:

namespace My_Project.Models
{
    [Serializable]
    public class Data
    {
        public string VIN { get; set; }
        public int ManufacturerId { get; set; }
        public SelectList Manufacturers { get; set; }
    }
}

控制器:

public ActionResult Temp()
{
    Data model = new Data
    {
        Manufacturers = DBAccess.getManufacturers()
    };

    Session["ModelData"] = model;

    return View(model);
}

[HttpPost]
public ActionResult Temp(Data newData)
{
    Data oldData = Session["ModelData"] as Data;

    oldData.ManufacturerId = 20;

    Session["ModelData"] = oldData;

    return View(oldData);
}

如果我在温度()设置ManufacturerId,那么我的下拉列表中正确与选定的任何生产厂家呈现。如果在后响应设置虽然,下拉列表中呈现的所有正确的选项,但没有正确的制造商选择。而如果你在视图看,我其实有它显示manufacturerId给我,以确保它是正确获取数据,并manufacturerId设置为在列表中的值,但它没有被选中。

If I set the ManufacturerId in Temp(), then my dropdown list renders correctly with whatever manufacturer selected. If it is set in the post response though, the dropdown list renders with all the correct options, but without the correct manufacturer selected. And if you look in the view, I actually have it displaying the manufacturerId to me to make sure it is getting the data correctly, and manufacturerId is set to a value that is in the list, but it is not selected.

我想不出有什么区别的,因为在渲染视图中使用的模型看起来完全相同的两个实例之间。最重要的是,如果POST方法是通过选择制造商(我在这一点上剥离出该功能)调用,它会返回相同的模型,但也正确呈现。

I can't figure out what the difference is between these two instances given that the model used in rendering the view looks identical. On top of that, if the post method is called by selecting the manufacturer (I have that functionality stripped out at this point), it would return the same model but also render correctly.

什么会导致此不从后返回正确呈现?

What would be causing this to not render correctly on the return from the post?

推荐答案

如果您需要设置一个值从控制器POST方法我觉得你需要用新的值更新的ModelState 。我想这是因为,即使你通过更新模型视图,ModelState中仍抱着旧值。

If you need to set a value from controller post method, I think you need to update the ModelState with the new value. I think it is because even if you pass updated model to the view, ModelState is still holding the old value.

试试这个:

[HttpPost]
public ActionResult Temp(Data newData)
{
    Data oldData = Session["ModelData"] as Data;
    oldData.ManufacturerId = 20;
    Session["ModelData"] = oldData;

    //Update model state with new ManufacturerId here
    CultureInfo myCulture = new System.Globalization.CultureInfo("en-GB");
    ModelState.SetModelValue("ManufacturerId", 
                             new ValueProviderResult((object)oldData.ManufacturerId, 
                                 oldData.ManufacturerId.ToString(), myCulture));

    return View(oldData);
}

这篇关于DropDownListFor并不总是显示选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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