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

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

MVC表單無法發(fā)布對象列表

MVC表單無法發(fā)布對象列表

慕仙森 2019-07-17 16:37:58
MVC表單無法發(fā)布對象列表所以我有一個MVC ASP.NET應(yīng)用程序有問題。本質(zhì)上,我有一個視圖,它包含一個表單,它的內(nèi)容被綁定到一個對象列表中。在這個循環(huán)中,它加載PartialView中正在循環(huán)的項?,F(xiàn)在一切都在運轉(zhuǎn),直到形體被提交。當(dāng)提交時,控制器將被發(fā)送一個空對象列表。下面的代碼證明了這些問題。家長意見:@model IEnumerable<PlanCompareViewModel>@using (Html.BeginForm("ComparePlans", "Plans", FormMethod.Post, new { id = "compareForm" })){<div>     @foreach (var planVM in Model)     {         @Html.Partial("_partialView", planVM)     }</div>}_ParalView:@model PlanCompareViewModel<div>     @Html.HiddenFor(p => p.PlanID)     @Html.HiddenFor(p => p.CurrentPlan)     @Html.CheckBoxFor(p => p.ShouldCompare)    <input type="submit" value="Compare"/></div>以下是上述代碼的類:PlanViewModel:public class PlansCompareViewModel{     public int PlanID { get; set; }     public Plan CurrentPlan { get; set; }     public bool ShouldCompare { get; set; }     public PlansCompareViewModel(Plan plan)     {         ShouldCompare = false;         PlanID = plan.PlanId;         CurrentPlan = plan;     }     public PlansCompareViewModel()     {         // TODO: Complete member initialization     }     public static IEnumerable<PlansCompareViewModel> CreatePlansVM(IEnumerable<Plan> plans)     {         return plans.Select(p => new PlansCompareViewModel(p)).AsEnumerable();     }}主計長:public class PlansController : MyBaseController{     [HttpPost]     public ActionResult ComparePlans(IEnumerable<PlanCompareViewModel> model)     {          //the model passed into here is NULL     }}問題在于控制器的作用。據(jù)我所知,它應(yīng)該發(fā)布一個PlanCompareViewModel的可枚舉列表,但它是空的。在檢查發(fā)送的POST數(shù)據(jù)時,它發(fā)送的是正確的Params。如果我要將‘IEnDigable’改為‘FormCollection’,它包含正確的值。有人知道為什么綁定器沒有創(chuàng)建正確的對象嗎?我可以使用javascript繞過這個問題,但這違背了目的!任何幫助都將不勝感激!
查看完整描述

2 回答

?
largeQ

TA貢獻(xiàn)2039條經(jīng)驗 獲得超8個贊

你的模型是null因為向表單提供輸入的方式意味著模型綁定器無法區(qū)分元素?,F(xiàn)在,這個代碼:

@foreach (var planVM in Model){
    @Html.Partial("_partialView", planVM)}

沒有為這些項目提供任何類型的索引。因此,它將反復(fù)生成HTML輸出,如下所示:

<input type="hidden" name="yourmodelprefix.PlanID" /><input type="hidden" name="yourmodelprefix.CurrentPlan" /><input type="checkbox" name="yourmodelprefix.ShouldCompare" />

但是,由于您希望綁定到集合,因此需要用索引命名表單元素,如:

<input type="hidden" name="yourmodelprefix[0].PlanID" /><input type="hidden" name="yourmodelprefix[0].CurrentPlan" /><input type="checkbox" name="yourmodelprefix[0].ShouldCompare" /><input type="hidden" name="yourmodelprefix[1].PlanID" /><input type="hidden" name="yourmodelprefix[1].CurrentPlan" /><input type="checkbox" name="yourmodelprefix[1].ShouldCompare" />

該索引使模型綁定器能夠關(guān)聯(lián)獨立的數(shù)據(jù)段,從而使其能夠構(gòu)造正確的模型。所以我建議你做些什么來解決這個問題。與其使用部分視圖遍歷集合,不如利用模板的強大功能。以下是您需要遵循的步驟:

  1. 創(chuàng)建一個

    EditorTemplates

    視圖當(dāng)前文件夾中的文件夾(例如,如果您的視圖是

    Home\Index.cshtml

    ,創(chuàng)建文件夾

    Home\EditorTemplates).

  2. 在該目錄中創(chuàng)建一個強類型視圖,其名稱與您的模型相匹配。在你的情況下

    PlanCompareViewModel.cshtml.

現(xiàn)在,您的部分視圖中的所有內(nèi)容都希望進(jìn)入該模板:

@model PlanCompareViewModel<div>
    @Html.HiddenFor(p => p.PlanID)
    @Html.HiddenFor(p => p.CurrentPlan)
    @Html.CheckBoxFor(p => p.ShouldCompare)
   <input type="submit" value="Compare"/></div>

最后,將父視圖簡化為:

@model IEnumerable<PlanCompareViewModel>@using (Html.BeginForm("ComparePlans", "Plans", FormMethod.Post, new { id = "compareForm" })){<div>
    @Html.EditorForModel()</div>}

DisplayTemplatesEditorTemplates足夠聰明,可以知道他們何時處理集合。這意味著它們將自動為表單元素生成正確的名稱,包括索引,以便正確建模綁定到集合。


查看完整回答
反對 回復(fù) 2019-07-17
  • 2 回答
  • 0 關(guān)注
  • 469 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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