0

我希望有人可以幫助這個。我有三個模型類是這樣的:MVC 3 SelectList/DropDownList不更新我的模型

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

正如你所看到的,一個運營商有一個位置(在其他地方重複使用單獨的類),和位置有一個狀態(這是空,直到選中)。

我的控制器看起來像這樣爲我的創建方法:

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 ... 
} 

最後,我的看法是這樣的:

<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的狀態永遠不會在我的模型上創建POST。我在我的編輯視圖中有類似的代碼,並且狀態在該視圖中正確填充(也就是說,與現有Provider.Location關聯的狀態顯示在DropDownList中選中,供用戶編輯,如果願望),但是在創建和編輯視圖由用戶所做的選擇決不會在我的模型(特別是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
@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) 
+0

非常感謝您!我現在理解API的各個部分要好得多,可能會選擇州。 – jlavallet