我已經在下面分享了視圖和視圖模型。我通過使用foreach
循環而不是使用某些@Html
幫助程序填充縮進的選擇列表(使用optgroup
)。但是,我不知道如何將列表中的選定值分配給CreateMainEntryViewModel
的特定屬性。更具體地說,我需要根據在提交表單時在下拉列表中進行的選擇來設置DiscussionWallId
和TimeFrameId
屬性值。在這個任務中,我使用Ajax.BeginForm()
:如何在Ajax.BeginForm()中手動設置屬性值()
@model EcholuMvc.Models.CreateMainEntryViewModel
@using (Ajax.BeginForm("CreateMainEntry", "MainEntry", new AjaxOptions
{
UpdateTargetId = "div_MainEntries",
InsertionMode = InsertionMode.Replace
}))
{
@Html.TextBoxFor(m => m.Title, new { placeholder = "Write a brief title...", @class = "form-control input-lg" })
@Html.TextAreaFor(m => m.Content, new { placeholder = "Type your entry here...", @class = "form-control mainEntryField" })
<label for="drp_DiscussionWallListForNewMainEntry" style="text-align:right" class="col-sm-3 control-label">Choose the Discussion Wall:</label>
<select id="drp_DWListForNewMainEntry" class="form-control">
@{
foreach (DiscussionWall dw in Model.DiscussionWalls)
{
<optgroup label="@dw.WallTitle">
foreach (TimeFrame tf in dw.TimeFrames)
{
<option data-dw="@dw.WallId" data-tf="@tf.TimeFrameId">@tf.TimeFrameName</option>
}
</optgroup>
}
}
</select>
<button type="submit" id="btn_SubmitNewPost">Post</button>
}
這是視圖模型:
public class CreateMainEntryViewModel
{
public string Title { get; set; }
public string Content { get; set; }
public Boolean IsAnonymous { get; set; }
public int DiscussionWallId { get; set; }
public int TimeFrameId { get; set; }
public CreateMainEntryViewModel(List<DiscussionWall> walls)
{
DiscussionWalls = walls;
}
public List< DiscussionWall> DiscussionWalls { get; set; }
}
我明白任何幫助!
您需要給'
@StephenMuecke您能否詳細說明一下?我不認爲有可能使用html助手對列表項進行分組和縮進。 – renakre
取決於哪個版本的MVC。如果您使用MVC-5,則支持分組。 –