2011-06-06 109 views
0

當我通過點擊「btSave」發佈時,在控制器中我收到該模型。在模型中,我看到所有字段的值(我在這裏僅顯示「FirstName」,但也有其他字段)。但是,doropdownlist值始終爲空。ASP.NET MVC發佈一個下拉列表

你知道爲什麼嗎?

感謝,

//Dropfown content 
public class LkpTypeCompany 
{ 
    public virtual int Id { get; set; } 
    public virtual int Code { get; set; } 
    public virtual string NL { get; set; } 
    public virtual string FR { get; set; } 
    public virtual string Value { get; set; } 
} 

//Model Definition 
public class CustomerModel 
{ 
    public List<LkpTypeCompany> LkpTypeCompany { get; set; } 
    public Customer Customer { get; set; } 
} 

//Posting form 
jQuery('#btGeneralSave').click(function (event) { 
    var jqxhr = $.post("Controller/Actio,", $("form").serialize(), 
    function (data) { 
    }); 
}); 

//HTML 
@model eSIT.GC.WebUI.Models.CustomerModel 
@using (Html.BeginForm()) 
{ 
    @Html.TextBoxFor(m => m.Customer.FirstName, new { maxlength = 50 }) 
    @Html.DropDownListFor(m => m.Customer.LkpTypeCompany, new SelectList(Model.LkpTypeCompany, "Code", "FR", Model.Customer.LkpTypeCompany.Code)) 
    <input type="button" id="btSave" value="Save"/> 
} 
+0

你不能DropDownListFor上使用屬性類型列表 http://msdn.microsoft.com/en-us/library/ee703436.aspx – Omu 2011-06-07 02:07:35

回答

1

我看到你試圖使用過載,但我有很好的魯奇使用SelectListItem

嘗試

@Html.DropDownListFor(m => m.Customer.LkpTypeCompany, 
    new SelectList(Model.LkpTypeCompany 
         .Select(i => new SelectListItem 
             { 
             Text = i.Code, 
             Value = (*somecondition*) ? i.FR : i.NL, 
             Selected = i.Code == Model.Customer.LkpTypeCompany.Code 
             })); 
+1

我有一些言論:1.我想避免Linq或cshml頁面中的其他C#代碼; 2.我在「SelectedListItem」上有錯誤; 3.它不是FR,可能是FR或NL,取決於用戶的語言,「FR」或「NL」文本將被複制到「Value」字段中 – 2011-06-06 20:09:54

+0

@ Kris-I它是一個錯字,實際上是'SelectListItem'而不是'SelectedListItem'。我不確定如何在沒有linq的情況下有條件地切換黑白FR和NL。查看我的編輯,並填寫條件以確定您是否要使用FR或NL。 – 2011-06-06 20:13:28

+1

Linq ok,但在cshtml頁面中沒有鏈接。與您的代碼在下拉列表中,我有這個「」沒有值的選項。在這個時候,我使用「DropDownList」和jQuery文章(通過jQuery獲取選定值) – 2011-06-06 20:27:19