jQuery中的ModelState.IsValid

时间:2012-06-04 07:45:15

标签: asp.net-mvc c#-4.0 jquery razor

我有一个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>

我的想法是检查jQuery脚本中的ModelState.IsValid,如果是Is.Valid,则显示imagePanel并检索ViewBag中的ProjectID

但是我不知道如何检索这个,如果最好的想法,或者可能有更好的想法。

感谢您的帮助和时间

1 个答案:

答案 0 :(得分:1)

最后,在看到这个优秀的tutorial

后,我选择了不引人注目的Ajax