0

我有一个 cshtml 表单,我在其中输入标题和文本,我希望实现的是,如果一切都有效,即数据输入成功,我会显示一个图像面板,并获取新条目的 ID,以便我可以在图像面板中使用它。

目前我有以下内容: -

控制器 :-

[HttpPost]
public ActionResult Create(Project project)
{
  var model = new CompositeImageModel(0);
  if (ModelState.IsValid)
  {
    model = new CompositeImageModel(project.ProjectID);
    ViewBag.ProjectID = project.ProjectID;
    db.Projects.Add(project);
    db.SaveChanges();

    return RedirectToAction("Index");
  }

  ViewBag.ProjectID = 0;

  return View(model);
}

CSHTML:=

<script type="text/javascript">
$(document).ready(function () {
  var imageList = $('#imageList'),
      submitButt = $('#submitButt');

  //hide the imageList initially
  imageList.hide(); //hide all but the first paragraph

  //when the user clicks on the create button, display the imageList
  submitButt.click(function () {
    imageList.fadeIn('slow');
  });
});
</script>

@using (Html.BeginForm())
{
  @Html.ValidationSummary(true)

  <fieldset>
    <legend>Project</legend>

    <div class="editor-label">
      @Html.LabelFor(model => model.projectModel.ProjectTitle)
    </div>
    <div class="editor-field">
      @Html.TextBoxFor(model => model.projectModel.ProjectTitle, new { style = "width:460px" })
      @Html.ValidationMessageFor(model => model.projectModel.ProjectTitle)
    </div>

    <div class="editor-label">
      @Html.LabelFor(model => model.projectModel.ProjectText)
    </div>
    <div class="editor-field">
      @Html.TextAreaFor(model => model.projectModel.ProjectText, new { cols = 55, @rows = 5 })
      @Html.ValidationMessageFor(model => model.projectModel.ProjectText)
    </div>

    <div id='submitButt'>
      <input type="submit" value="Create" />
    </div>
  </fieldset>
}

<div id='imageList'>
  <h2>Upload Image(s)</h2>

  @{ Html.RenderPartial("~/Views/File/ImageUpload.cshtml", new MvcCommons.Models.CompositeImageModel((int)ViewBag.ProjectID)); }

</div>
<div>
  @Html.ActionLink("Back to List", "Index")
</div>

我的想法是检查ModelState.IsValidjQuery 脚本内部,如果是 Is.Valid,则显示 imagePanel 并检索ProjectIDViewBag 内部。

但是我不确定如何检索它,如果它毕竟是最好的主意,或者可能有更好的主意。

感谢您的帮助和时间

4

1 回答 1

1

最后,在看到这个优秀的教程后,我选择了 Unobtrusive Ajax

于 2012-06-04T09:25:31.847 回答