![](/img/trans.png)
[英]How to pass Model List from Controller to View with Jquery or Javascript
[英]How to pass Model from View to Controller, add items to list, pass back to view
每次單擊按鈕時,我都會在表單中添加一個局部視圖。 它是一個帶有一組響應的文本區域。 如何將我的模型從視圖傳遞到控制器,將其添加到列表模型並返回視圖?
我正在傳遞 ReviewFormViewModel 我想將 ListAdhoc 傳遞給部分控制器並向其添加項目,然后將其傳遞回視圖。
public class ReviewFormViewModel
{
...// other fields
public List<AdhocViewModel> ListAdhoc { get; set; }
}
public class AdhocViewModel
{
public int? ReviewId { get; set; }
public String AdhocQuestion { get; set; } //free form
public int? SelectedAnswer { get; set; } // for binding int? for optional
public String Comments { get; set; }
public List<AdhocOptionsVM> ListAdhocOptions { get; set; }
}
public class AdhocOptionsVM
{
public int AnswerId { get; set; }
public String RatingName { get; set; }
public Decimal Rating { get; set; }
public String ActiveFl { get; set; }
}
局部視圖控制器
public PartialViewResult Adhoc()
{
//pass model object on button click and add each item to the model everytime
var AdhocObj = new AdhocViewModel();
AdhocObj.ListAdhocOptions = new List<AdhocOptionsVM>();
var query = db.dbQuestionOptions.Where(qo => qo.ActiveFl == "Y").OrderByDescending(qo => qo.Rating).ToList();
foreach (var item in query)
{
var AdhocAnsOptionsVMObj = new AdhocOptionsVM();
AdhocAnsOptionsVMObj.AnswerId = item.AnswerId;
AdhocAnsOptionsVMObj.RatingName = item.RatingName;
AdhocAnsOptionsVMObj.Rating = item.Rating;
AdhocAnsOptionsVMObj.ActiveFl = item.ActiveFl;
AdhocObj.ListAdhocOptions.Add(AdhocAnsOptionsVMObj);
}
return PartialView("Adhoc", AdhocObj);
}
以及使用 ReviewFormViewModel 的部分視圖:
<div class="adhoc">
@using (Html.BeginCollectionItem("adhoc"))
{
<div class="panel panel-success">
<div class="panel-heading">
@Html.HiddenFor(m => m.ReviewId)
@Html.HiddenFor(m => m.AdhocId)
@Html.TextAreaFor(m => m.AdhocQuestion, htmlAttributes: new { @style = "width:650px", @placeholder = "Enter Adhoc Question here" })<br />
</div>
<div class="panel-body">
@foreach (var optAnswer in Model.ListAdhocOptions)
{
<div class="radio">
<responselabel>@Html.RadioButtonFor(m => m.SelectedAnswer, optAnswer.AnswerId, new { id = optAnswer.AnswerId }) @optAnswer.RatingName</responselabel><br />
</div>
}
<div>@Html.ValidationMessageFor(m => m.SelectedAnswer)</div><br />
@Html.TextAreaFor(m => m.Comments, htmlAttributes: new { @style = "width:650px", @placeholder = "Comments" })<br /><br />
</div>
<button type="button" class="delete">Delete</button>
</div>
}
主視圖
@model CustomerFeedback.Areas.ProjectManagers.Models.ReviewFormViewModel
@{
ViewBag.Title = "CreateFormsIndex";
}
<h4 align="center">Project Review Form</h4>
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="text-center">
<h4>
@Html.DisplayName(Model.ProjectId) @Html.DisplayName(Model.ProjectName)
</h4>
<h4>
PM: @Html.DisplayName(Model.FullName)
</h4>
</div>
</div>
</div>
</div>
<div class="container">
<br />
<div class="panel-group">
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(m => m.ProjectId)
@Html.HiddenFor(m => m.AccountId)
@Html.HiddenFor(m => m.ReviewDate)
<div class="panel panel-default">
<div class="panel-body">
<div class="panel-group">
<div class="panel-heading">
<h4 class="panel-title">
Required Questions
</h4>
</div>
@for (int i = 0; i < Model.ListReqQuestions.Count; i++)
{
<div class="panel panel-success">
<div class="panel-heading">
@Html.HiddenFor(m => m.ListReqQuestions[i].QuestionId)
@Html.DisplayFor(m => m.ListReqQuestions[i].QuestionText)
</div>
<div class="panel-body">
@foreach (var optAnswer in Model.ListReqQuestions[i].ListQuestionOptions)
{
<div class="radio">
<responselabel>@Html.RadioButtonFor(m => m.ListReqQuestions[i].SelectedAnswer, optAnswer.AnswerId, new { id = optAnswer.AnswerId }) @optAnswer.RatingName</responselabel><br />
</div>
}
<div>@Html.ValidationMessageFor(m => m.ListReqQuestions[i].SelectedAnswer)</div><br />
@Html.TextAreaFor(m => m.ListReqQuestions[i].Comments, htmlAttributes: new { @style = "width:650px", @placeholder = "Comments" })<br /><br />
</div>
</div>
}
<div class="panel-heading">
<h4 class="panel-title">
Optional Questions
</h4>
</div>
@for (int i = 0; i < Model.ListOpQuestions.Count; i++)
{
<div class="panel panel-success">
<div class="panel-heading">
@Html.HiddenFor(m => m.ListOpQuestions[i].QuestionId)
@Html.DisplayFor(m => m.ListOpQuestions[i].QuestionText)
</div>
<div class="panel-body">
@foreach (var optAnswer in Model.ListOpQuestions[i].ListQuestionOptions)
{
<div class="radio">
<responselabel>@Html.RadioButtonFor(m => m.ListOpQuestions[i].SelectedAnswer, optAnswer.AnswerId, new { id = optAnswer.AnswerId }) @optAnswer.RatingName</responselabel><br />
</div>
}
<div>@Html.ValidationMessageFor(m => m.ListOpQuestions[i].SelectedAnswer)</div><br />
@Html.TextAreaFor(m => m.ListOpQuestions[i].Comments, htmlAttributes: new { @style = "width:650px", @placeholder = "Comments" })<br /><br />
</div>
</div>
}
@*on click (new adhoc question) add a new freeform question with list of answers*@
<div class="panel panel-success" id="adhoc">
@* renders partial adhoc view *@
</div>
<br />
<div class="center">
<input type="button" value="New Adhoc Question" class="btnAdhoc btn-success" />
</div>
<br />
<div class="center">
<input type="submit" value="Save" name="Command" class="btn btn-success" />
<input type="submit" value="Submit" name="Command" class="btn btn-success" />
<input type="submit" value="Cancel" name="Command" class="btn btn-success" />
<input type="submit" value="Attach" name="Command" class="btn btn-success" />
</div>
</div>
</div>
</div>
}
</div>
</div>
<script>
$(function () {
$('.btnAdhoc').click(function (event) {
event.preventDefault();
$.ajax({
url: '/ProjectManagers/Forms/Adhoc',
//data: JSON.stringify(model),
type: 'get',
success: function (result) {
$('#adhoc').append(result);
}
});
});
})
</script>
更新:我添加了 AdhocViewModel。
我已經為這些屬性添加了視圖模型。 我有一個表格,其中包含一組要回答的問題和答案。 那些來自數據庫。 我有一個按鈕,單擊將生成局部視圖並附加到表單(可以是多個)。 局部視圖由一個文本區域(用於輸入的任何問題)、一組響應(來自數據庫)和一個評論框組成。 我不知道如何在發布(提交)時處理這個問題。 我的嘗試是將模型從視圖傳遞到局部控制器,向其中添加項目並將其傳遞回視圖進行處理。 我在傳遞模型數據方面沒有任何成功
更新 2使用 BeginCollectionItem 幫助程序更新了代碼。 添加了主視圖
集合未綁定的原因是因為BeginCollectionItem()
的參數必須與您的屬性名稱匹配。 將其更改為
@using (Html.BeginCollectionItem("ListAdhoc")) // binds to List<AdhocViewModel> ListAdhoc
此外,您還需要在主視圖中使用循環來呈現AdhocViewModel
中的現有AdhocViewModel
。 即使最初不存在,如果由於ModelState
無效而需要返回視圖,仍然需要它。 在主視圖中包括
<div class="panel panel-success" id="adhoc">
@foreach(var item in Model.ListAdhoc)
{
@Html.Partial("Adhoc", item)
}
</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.