私は次のアクションメソッドを持つホームコントローラーを持っています。
[HttpPost]
[Authorize(Roles = "Admin")]
public JsonResult ProjectAdd(PortfolioViewModel model, int[] categories)
{
using (PortfolioManager pm = new PortfolioManager())
{
using (CategoryManager cm = new CategoryManager())
{
if (ModelState.IsValid)
{
bool status = pm.AddNewProject(model, categories);
if (status)
{
// ViewBag.Message = "Inserted Sucessfully :)";
}
}
}
return Json(pm.GetAllProjects());
}
}
私のビューは「ProjectAdd.CSHTML」と呼ばれ、次のコンテンツが含まれています。
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<thead>
// the heading goes here
</thead>
<tbody id="projectsUpdate">
@Html.Partial("_ProjectsPartial", ViewBag.ProjectsList as IEnumerable<MVC3Resume.Models.PortfolioViewModel>)
</tbody>
</table>
<p>
@using (Ajax.BeginForm("projectAdd", "home", new AjaxOptions {
LoadingElementId = "loading",
LoadingElementDuration = 2000,
Url = Url.Action("projectAdd", "home"),
OnSuccess="OnSucess",
},
new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<div id="SucessMessage"></div>
<fieldset>
<legend>Add New Project</legend>
<div class="editor-label">
@Html.LabelFor(model => model.ProjectHeading)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ProjectHeading)
@Html.ValidationMessageFor(model => model.ProjectHeading)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.Image)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.Image, new { type = "file" })
@Html.ValidationMessageFor(model => model.Image)
</div>
<div class="editor-label">
<label for="categories">Categories</label>
</div>
<div class="editor-label">
@foreach (var item in ViewBag.Categories as IEnumerable<MVC3Resume.Models.CategoryViewModel>)
{
<input type="checkbox" name="categories" value="@item.CategoryId" />
@item.CategoryName
}
</div>
<p>
<input type="submit" value="Create" class="submit" />
</p>
</fieldset>
}
</p>
私のjavascriptOnSucess関数は次のようになります。
<script type="text/javascript">
function OnSucess(resultData) {
// all logic goes here
}
</script>
だから、私の質問は、jsonの結果データを取得して部分ビューに入力するにはどうすればよいですか? 以下は私の結果のjsonデータです。
[{"ProjectId":1,"ProjectHeading":"heading","ProjecctUrl":"url","ProjectLongDescription":"descripitn","ProjectShortDescription":"...","PromoFront":false,"Thumbnail":null,"ProjectThubmnail":"folio_140_1.jpg","Image":null,"ProjectImage":"folio_preview640.jpg","CategoryId":2},{"ProjectId":2,"ProjectHeading":"heading 2","ProjecctUrl":"url2","ProjectLongDescription":"description 2","ProjectShortDescription":"...","PromoFront":false,"Thumbnail":null,"ProjectThubmnail":"folio_140_2.jpg","Image":null,"ProjectImage":"folio_preview640.jpg","CategoryId":1},{"ProjectId":3,"ProjectHeading":"teasting heading","ProjecctUrl":"test url","ProjectLongDescription":"description","ProjectShortDescription":"...","PromoFront":false,"Thumbnail":null,"ProjectThubmnail":"folio_preview640.jpg","Image":null,"ProjectImage":"folio_preview640.jpg","CategoryId":1}]