第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

MVC3 DropDownListFor-一個(gè)簡單的例子?

MVC3 DropDownListFor-一個(gè)簡單的例子?

萬千封印 2019-09-21 14:37:54
我DropDownListFor在MVC3應(yīng)用中遇到問題。我能夠使用StackOverflow弄清楚如何使它們出現(xiàn)在View上,但是現(xiàn)在我不知道如何在提交視圖模型時(shí)捕獲其相應(yīng)屬性中的值。為了使它起作用,我必須創(chuàng)建一個(gè)具有ID和value屬性的內(nèi)部類,然后必須使用an IEnumerable<Contrib>來滿足DropDownListFor參數(shù)要求。但是,現(xiàn)在,MVC FW應(yīng)該如何將在此下拉列表中選擇的值映射回我的視圖模型的simple string屬性中?public class MyViewModelClass{    public class Contrib    {        public int ContribId { get; set; }        public string Value { get; set; }    }    public IEnumerable<Contrib> ContribTypeOptions =         new List<Contrib>        {            new Contrib {ContribId = 0, Value = "Payroll Deduction"},            new Contrib {ContribId = 1, Value = "Bill Me"}        };    [DisplayName("Contribution Type")]    public string ContribType { get; set; }}在我的視圖中,將下拉列表放置在頁面上,如下所示:<div class="editor-label">    @Html.LabelFor(m => m.ContribType)</div><div class="editor-field">    @Html.DropDownListFor(m => m.ContribTypeOptions.First().ContribId,              new SelectList(Model.ContribTypeOptions, "ContribId", "Value"))</div>當(dāng)我提交表單時(shí)ContribType(當(dāng)然)為null。正確的方法是什么?
查看完整描述

3 回答

?
慕沐林林

TA貢獻(xiàn)2016條經(jīng)驗(yàn) 獲得超9個(gè)贊

您應(yīng)該這樣做:


@Html.DropDownListFor(m => m.ContribType, 

                new SelectList(Model.ContribTypeOptions, 

                               "ContribId", "Value"))

哪里:


m => m.ContribType

是結(jié)果值所在的屬性。


查看完整回答
反對 回復(fù) 2019-09-21
?
明月笑刀無情

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超4個(gè)贊

我認(rèn)為這會有所幫助:在Controller中獲取列表項(xiàng)和選定的值


public ActionResult Edit(int id)

{

    ItemsStore item = itemStoreRepository.FindById(id);

    ViewBag.CategoryId = new SelectList(categoryRepository.Query().Get(), 

                                        "Id", "Name",item.CategoryId);


    // ViewBag to pass values to View and SelectList

    //(get list of items,valuefield,textfield,selectedValue)


    return View(item);

}

并在視野中


@Html.DropDownList("CategoryId",String.Empty)


查看完整回答
反對 回復(fù) 2019-09-21
?
阿波羅的戰(zhàn)車

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超6個(gè)贊

要將動(dòng)態(tài)數(shù)據(jù)綁定到DropDownList中,可以執(zhí)行以下操作:


如下所示在Controller中創(chuàng)建ViewBag


ViewBag.ContribTypeOptions = yourFunctionValue();

現(xiàn)在在如下所示的視圖中使用該值:


@Html.DropDownListFor(m => m.ContribType, 

    new SelectList(@ViewBag.ContribTypeOptions, "ContribId", 

                   "Value", Model.ContribTypeOptions.First().ContribId), 

    "Select, please")


查看完整回答
反對 回復(fù) 2019-09-21
  • 3 回答
  • 0 關(guān)注
  • 423 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號