有這樣的堆棧上有很多問題,但沒有完全解決我的問題。模型是空的Html.BeginForm
我有各種性質的模型,其中之一是一個列表:
public class MyModel
{
....
public List<ValuesViewModel>Values { get; set; }
}
其中ValuesViewModel
是:
public class ValuesViewModel
{
public string Name { get; set; }
public string StatusOne { get; set; }
public string StatusTwo { get; set; }
}
我使用下拉successfuly設法輸出它們在我看來列表來修改這樣的值:
@using (Html.BeginForm("GenerateReport", "Job", method: FormMethod.Post))
{
<div class="container">
@{
List<SelectListItem> statuses = new List<SelectListItem>();
statuses.Add(new SelectListItem
{
Text = "Bad",
Value = "Bad"
});
statuses.Add(new SelectListItem
{
Text = "Fair",
Value = "Fair",
Selected = true
});
statuses.Add(new SelectListItem
{
Text = "Good",
Value = "Good"
});
for (int i = 0; i < Model.Values.Count; ++i)
{
@Html.EditorFor(model => Model.Values[i].Name)
<p>Status One @Html.DropDownListFor(model => Model.Values[i].StatusOne, Values)</p>
<p>Status Two @Html.DropDownListFor(model => Model.Values[i].StatusTwo, Values)</p>
}
}
</div>
<button type="submit" class="btn btn-custom saveButtons ">Create report</button>
}
這樣一切正常,模型發佈到我的控制器方法"GenerateReport"
列表數據,我很高興。但! 我有幾個按鈕對應於幾種控制器方法,它們都使用來自List<ValuesViewModel>Values
的數據。 如果我把這個代碼塊:
@HTML.BeginForm
for (int i = 0; i < Model.Values.Count; ++i)
{
@Html.EditorFor(model => Model.Values[i].Name)
<p>Status One @Html.DropDownListFor(model => Model.Values[i].StatusOne, Values)</p>
<p>Status Two @Html.DropDownListFor(model => Model.Values[i].StatusTwo, Values)</p>
}
,在我的控制器方法的模式將是完全無效即:
public ActionResult GenerateReport(MyModel model)
{
// model is null here!
}
public ActionResult GenerateShortReport(MyModel model)
{
// model is null here!
}
這裏的最愚蠢的解決方案是使用BeginForm
每個按鈕,但我希望並相信還有另一種方式。 我怎樣才能解析我的列表值到幾個控制器方法,而無需重複每個按鈕的整個下拉列表?
你嘗試'[HttpPost]'和'[FromBody]'屬性? – ASpirin
@StephenMuecke這就是我所需要的,現在它完美的工作!謝謝。 它是如此複雜的網絡.. – Amarula