[英].NET Core razor submit hidden list in form to controller model
[英]Add Hidden Model List To Razor Form
我正在創建向導,並且需要傳播已創建的列表。 該列表屬於我的聯系人模型。 我需要將其放在表單中,這樣當我在最后一步中發回控制器時,我可以將聯系人列表作為向導模型的一部分(它現在僅返回null)。 對於其他簡單表單對象,我使用HiddenFor將數據保留在模型中。 這是行不通的,因為隱藏的for只是一個文本框,並且我有一個復雜的對象。
我考慮過創建一個DropDownListFor並將其包裝在一個隱藏的div中。 這只是拼湊而成,實際上需要花點時間才能使DropDownListFor接受另一個對象類型(如果它可以工作的話)。
我已經考慮過將列表保存到Session [“ Contacts”],但這似乎不是正確的MVC方法。 有沒有人有更好的建議?
謝謝,TJ
-更新-
作為對merekel的回應,我將添加更多詳細信息以闡明問題所在。 該列表未被用戶修改,並且不會顯示在表單上。 我在向導步驟之一中創建列表,並將其保存到模型中,如下所示。
查看模型
public class WizardViewModel
{
//...
public List<Contact> ContactList { get; set; }
//...
}
控制器代碼
//Step 4 in the wizard
model.ContactList = CALL FUNCTION THAT CREATES LIST BASED ON FORM SELECTIONS
//other calculations needed for step 5
//Save Contacts for later
Session["Contacts"] = model.ContactList;
//Step 5 in the wizard
//Readd the contacts to the model
model.ContactList = (List<Contact>)Session["Contacts"];
//Save the model to pass to the report page
//My report viewer is on an ASPX page so I am passing all the data here with a session
Session["ReportModel"] = model;
會話保存按預期工作。 似乎我不應該使用會話使用MVC在頁面之間保存數據。 從MVC到ASPX時,我需要這樣做,所以我僅擔心Session [“ ReportModel”] Session [“ Contacts”]。 感謝您的任何建議。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.