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

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

模型綁定到列表MVC 4

模型綁定到列表MVC 4

是否有將IList項(xiàng)綁定到視圖的模式。我似乎遇到了HttpPost的問(wèn)題。我知道菲爾哈克寫(xiě)了一篇很好的文章,但它已經(jīng)過(guò)時(shí)了,他說(shuō)他們可能會(huì)修復(fù)MVC 4。
查看完整描述

3 回答

?
繁花如伊

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

一個(gè)干凈的解決方案可以創(chuàng)建一個(gè)通用類來(lái)處理列表,因此您不需要在每次需要時(shí)創(chuàng)建不同的類。


public class ListModel<T>

{

    public List<T> Items { get; set; }


    public ListModel(List<T> list) {

        Items = list;

    }

}

當(dāng)您返回視圖時(shí),您只需要執(zhí)行以下操作:


List<customClass> ListOfCustomClass = new List<customClass>();

//Do as needed...

return View(new ListModel<customClass>(ListOfCustomClass));

然后在模型中定義列表:


@model ListModel<customClass>

準(zhǔn)備好了:


@foreach(var element in Model.Items) {

  //do as needed...

}


查看完整回答
反對(duì) 回復(fù) 2019-09-03
?
慕尼黑5688855

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

?控制器


namespace ListBindingTest.Controllers

{

    public class HomeController : Controller

    {

        //

        // GET: /Home/


        public ActionResult Index()

        {

            List<String> tmp = new List<String>();

            tmp.Add("one");

            tmp.Add("two");

            tmp.Add("Three");

            return View(tmp);

        }


        [HttpPost]

        public ActionResult Send(IList<String> input)

        {

            return View(input);

        }    

    }

}

?強(qiáng)類型索引視圖


@model IList<String>


@{

    Layout = null;

}


<!DOCTYPE html>


<html>

<head>

<meta name="viewport" content="width=device-width" />

<title>Index</title>

</head>

<body>

    <div>

    @using(Html.BeginForm("Send", "Home", "POST"))

    {

        @Html.EditorFor(x => x)

        <br />

        <input type="submit" value="Send" />

    }

    </div>

</body>

</html>

?強(qiáng)類型發(fā)送視圖


@model IList<String>


@{

    Layout = null;

}


<!DOCTYPE html>


<html>

<head>

<meta name="viewport" content="width=device-width" />

<title>Send</title>

</head>

<body>

    <div>

    @foreach(var element in @Model)

    {

        @element

        <br />

    }

    </div>

</body>

</html>

這就是你必須做的所有事情,將他的MyViewModel模型更改為IList。


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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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