MVC 3 Selectlist / DropDownList未更新我的模型

时间:2011-09-28 16:39:06

标签: asp.net-mvc-3 model-view-controller drop-down-menu selectlist

我希望有人可以帮助解决这个问题。我有三个这样的Model类:

public class Provider
{
    public Guid ProviderId { get; set; }
    public string Name { get; set; }

    public Guid LocationId { get; set; }

    public virtual Location Location { get; set; }
}

public class Location
{
    public Guid LocationId { get; set; }
    public string NameOrCode { get; set; }
    public string Description { get; set; }
    public string StreetNumber { get; set; }
    public string StreetAddress1 { get; set; }
    public string StreetAddress2 { get; set; }
    public string City { get; set; }

    public int? StateId { get; set; }

    public string Zip { get; set; }
    public string ContactPhone { get; set; }

    public virtual State State { get; set; }
}

public class State
{
    public int StateId { get; set; }
    public string Name { get; set; }
    public string Abbreviation { get; set; }
}

正如您所看到的,Provider有一个Location(单独的类可以在其他地方重用),而Location有一个State(在选中之前为null)。

对于我的Create方法,我的控制器看起来像这样:

public class ProviderController : BaseController
{
    private SetupContext db = new SetupContext();

    // other CRUD methods ...

    //
    // GET: /Provider/Create

    public ActionResult Create()
    {
        Location location = new Location() 
        { 
            LocationId = Guid.NewGuid(), 
            NameOrCode = Resources.BillingLocation,
            Description = Resources.BillingLocationDescription
        };
        Provider provider = new Provider()
        {
            ProviderId = Guid.NewGuid(),
            LocationId = location.LocationId,
            Location = location
        };
        ViewBag.StateId = new SelectList(db.States, "StateId", "Name", provider.Location.StateId);

        return View(provider);
    } 

    //
    // POST: /Provider/Create

    [HttpPost]
    public ActionResult Create(Provider provider)
    {
        if (ModelState.IsValid)
        {
            db.Locations.Add(provider.Location);
            db.Providers.Add(provider);
            db.SaveChanges();

            return RedirectToAction("Index");  
        }

        ViewBag.StateId = new SelectList(db.States, "StateId", "Name", provider.Location.StateId);
        return View(provider);
    }

    // other CRUD methods ... 
}

最后,我的View看起来像这样:

<div class="editor-label">
    @Html.LabelFor(model => model.Location.StateId, @Resources.Location_State_Display_Name)
</div>
<div class="editor-field">
    @Html.DropDownList("StateId", @Resources.ChooseFromSelectPrompt)
    @Html.ValidationMessageFor(model => model.Location.StateId)
</div>

我的问题是用户在DropDownList中选择的状态永远不会在Create POST上的Model上设置。我的编辑视图中有类似的代码,并且在该视图中正确填充了状态(即,与DropDownList中选择的现有Provider.Location相关联的状态,以便用户在需要时进行编辑),但是在创建和编辑视图用户所做的选择从未在我的模型(特别是来自POST的Provider.Location.StateId)中注册。

查看生成的HTML,我看到了:

<div class="editor-label">
    <label for="Location_StateId">State/Territory</label>
</div>
<div class="editor-field">
    <select id="StateId" name="StateId"><option value="">[Choose]</option>
        <option value="1">Alabama</option>
        <option value="2">Alaska</option>
        <!-- more options ... -->
    </select>
    <span class="field-validation-valid" data-valmsg-for="Location.StateId" data-valmsg-replace="true"></span>
</div>

我怀疑我需要以某种方式传达Location.StateId关系,而不仅仅是我在上面看到的StateId,但我无法弄清楚这样做的正确语法。我已经尝试将ViewBag动态属性更改为Location_StateId,如下所示:

ViewBag.Location_StateId = new SelectList(db.States, "StateId", "Name", provider.Location.StateId);

我视图中的DropDownList如下:

@Html.DropDownList("Location_StateId", @Resources.ChooseFromSelectPrompt)

我想或许符号会起作用,因为我的DropDownList旁边的标签呈现为:

<div class="editor-label">
    <label for="Location_StateId">State/Territory</label>
</div>

这种尝试不起作用。你能救我一下吗?

提前致谢。

1 个答案:

答案 0 :(得分:1)

@Html.DropDownList("Location.StateId", @Resources.ChooseFromSelectPrompt)

以下行也没有用处:

ViewBag.StateId = new SelectList(db.States, "StateId", "Name", provider.Location.StateId);

您正在将SelectList分配给应该是标量属性的东西。您可能希望将该集合作为ViewBag传递:

ViewBag.States = new SelectList(db.States, "StateId", "Name", provider.Location.StateId);

然后在视图中:

@Html.DropDownList("Location.StateId", (SelectList)ViewBag.States)