Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ASP.NET MVC3:模式对话框中的编辑视图_Javascript_Jquery_Asp.net_Asp.net Mvc 3_Razor - Fatal编程技术网

Javascript ASP.NET MVC3:模式对话框中的编辑视图

Javascript ASP.NET MVC3:模式对话框中的编辑视图,javascript,jquery,asp.net,asp.net-mvc-3,razor,Javascript,Jquery,Asp.net,Asp.net Mvc 3,Razor,我们正在编写一个ASP.NET MVC3应用程序,并希望它“丰富”,例如使用引导和模式对话框 我现在想知道如何实现模态对话框,而不破坏ASP.NET(ModelErrors,…)中所有优秀的人员 工作流程应如下所示: IndexView带有一个项目列表,每个项目都有一个显示模式对话框的actionlink @Ajax.ActionLink( "Edit", // Link Text "Edit", // ActionMethod new

我们正在编写一个ASP.NET MVC3应用程序,并希望它“丰富”,例如使用引导和模式对话框

我现在想知道如何实现模态对话框,而不破坏ASP.NET(ModelErrors,…)中所有优秀的人员

工作流程应如下所示:

  • IndexView带有一个项目列表,每个项目都有一个显示模式对话框的actionlink

    @Ajax.ActionLink( 
             "Edit",  // Link Text
             "Edit",  // ActionMethod
             new { id = item.Id }, // RouteValues
             new AjaxOptions { 
                      HttpMethod = "Get", 
                      OnBegin = "modal.showModalDiv()", 
                      InsertionMode = InsertionMode.Replace, 
                      UpdateTargetId = "modal-div", 
                      OnSuccess = "modal.ajaxSuccess()" }, 
             new { data-toggle = "edit-modal" } // HTML-Attributes
    )
    
  • 模态对话框(使用css样式的简单div)呈现editview(从控制器actionmethode返回)

  • 编辑视图中的表单可用于编辑项目,包括客户端验证

    @{
         AjaxOptions ajaxOptions = new AjaxOptions() {
              HttpMethod = "Post", OnSuccess="modal.hideModalDiv()"
         };
    }
    @using (Ajax.BeginForm("Edit"), ajaxOptions){
    
    
        ... element to edit item ...
    
        <input type="submit" value="submit" />
    }
    
  • 我的问题是:如何实施步骤4?有人有什么建议吗?

    试试这个

    [HttpPost]
    public ActionResult Edit(Guid id, ItemModel model) 
    {
      if(model != null && ModelState.IsValid)
      {
       return RedirectToAction("Index")
      }
      else
      {
       return PartialView(model);
      }
    
    }
    

    我想你已经解决了这个问题或者找到了解决方法,但是如果你没有,我怀疑如果你返回一个包含javascript的局部视图来进行重定向,你可以得到你描述的行为

    例如:

    重定向到index.cshtml

    @{ Layout = null; }
    <script type="text/javascript">
      window.location.href = "@Url.Action("Index")";
    </script>
    

    不是最优雅的,但应该有效,我说应该,因为我没有测试过这个…

    嗨,谢谢你的回答。我想你没有理解我的问题。你的建议看起来(除了ModelState.IsValid)和我的完全一样。我的问题是,如何处理不同类型的响应(PartialViewResult替换div与Redirectresult重定向或替换另一个div)?
    [HttpPost]
    public ActionResult Edit(Guid id, ItemModel model) 
    {
      if(model != null && ModelState.IsValid)
      {
       return RedirectToAction("Index")
      }
      else
      {
       return PartialView(model);
      }
    
    }
    
    @{ Layout = null; }
    <script type="text/javascript">
      window.location.href = "@Url.Action("Index")";
    </script>
    
    [HttpPost]
    public PartialViewResult Edit(Guid id, ItemModel model) {
    try{
          //Save Item ...
          return PartialView("RedirectToIndex")
        } catch (Exception ex){
          ModelState.AddModelError("", "An error occured");
          return PartialView(model);
        }
    }