2014-09-10 38 views
0

情况:在我的C#/ MVC 4解决方案中,我正在使用一个视图,其中包含部分视图。该视图是一个带有提交按钮的表单。局部视图是隐藏的div,但是如果选中复选框,则可以显示该div。MVC 4局部视图导致页面在提交时变得无响应

问题:如果部分视图隐藏,则提交正常工作。如果部分视图未隐藏,则提交会导致页面无响应,如果等待3分钟左右,则提交最终会按预期工作。

代码如下。提前感谢您的考虑。我是新手开发人员,因此欢迎所有评论,建议和批评。

代码:

型号

namespace MyModels 
{ 
    public class MainModel 
    { 
     public SelectListItem Things { get; set;} 

     public IEnumerable<OtherModel> MoreThings { get; set;} 
    } 
} 

查看 //名为MyView的 @model MyModels.MainModel @using MyModels @if(型号!= NULL){

using (Html.BeginForm("MyViewName", "MyControllerName", FormMethod.Post, new { id = "view-form" })) 
{ 
    @Html.LabelFor(model => model.things) 
    @Html.DropDownList("", (Selectist)ViewBag.things) 
    @Html.ValidationMessageFor(model => model.field1) 

    @Html.CheckBoxWithLabel("aNameAttribute", Model.valueAttribute.ToString(), "anIdAttribute", Model.valueAtttribue ==1, "aLabel", "a_Toggle_Class") 
    <div class="treeview" style="display: none;"> 
    <fieldset> 
     <legend>Title</legend> 
    //view causing issues replaces the div below 
    <div id="replacedDiv"></div> 
    </fieldset> 
    </div> 

    <p> 
     <input type="submit" value="Submit" /> 
    </p> 
} 

}

<script type="text/javascript"> 
    $(document).ready(function() { 
     $.ajax({ 
      url: "/MyController/MyPartialView", 
      contentType: "application/html; charset=utf-8", 
      cache: "false", 
      type: "GET", 
      datatype: "html" 
     }) 
     .success(function (result) { 
      $('#replacedDiv").html(result); 
     }) 
    }); 
</script> 

管窥

//named _MyPartialView 
@model MyModels.MainModel 
@using MyModels 

@foreach (var moreThings in ViewBag.moreThings) 
{ 
    <div id="replacedDiv"> 
    <label> 
    <input type="checkbox" [email protected] [email protected] />@moreThings.name </label> 
    </div> 
} 

控制器

namespace Main.Controllers 
{ 
    public class MyController 
    { 
     [HttpGet] 
     public ActionResult Index(MainModel model) 
     { 
      return View(model); 
     } 

     public ActionResult MyView() 
     { 
      var model = new MainModel(); 

      return View(model); 

     } 

     public ActionResult MyPartialView(MainModel model) 
     { 
      <OtherModel> moreThings = BLotherModel.GetMoreThings(); 
      ViewBag.moreThings = moreThings; 

      return PartialView("_MyPartialView", promotion); 
     } 

     [HttpPost] 
     public ActionResult MyView(FormCollection collection) 
     { 
      MainModel model = new MainModel(); 

      return SaveModel(model); 
     } 
    } 
} 
+1

你在你的JavaScript有语法错误。虽然这可能不会导致这个具体问题,但它肯定没有帮助。 – David 2014-09-10 17:08:42

+0

删除代码中的某些元素以找出根本原因。 – Rohit 2014-09-10 17:09:50

+0

我刚更新的JavaScript,这是一个错字,而不是在实际的代码 – 2014-09-10 17:12:48

回答

2

在你的Ajax使用的是:

$('#replacedDiv").html(result); 

但是你的局部视图包含在一个循环中产生的<div id="replacedDiv">

替换为您的局部视图代码:

@foreach (var moreThings in ViewBag.moreThings) 
{ 
    <label>@moreThings.name </label> 
    <input type="checkbox" [email protected] [email protected] /> 
} 

,它应该是OK

+0

问题仍然存在,但是我很好地使用了此建议的更改。它彻底摆脱了我的注意。 – 2014-09-10 19:35:28

+1

尝试打开fiddler或其他网络监视器(浏览器中的浏览器也可以)并检查POST请求何时发送?如果请求立即发送,则问题在服务器端。 在您的发布操作的第一行放置一个断点。如果请求提交和中断点之间存在延迟,那么问题可能出在模型联编程序/操作过滤器或其他在执行本身之前执行的其他任何东西。 – 2014-09-10 19:45:23

+0

确实如此奇怪的行为不会发生在Firefox中,所以我要继续并将原始建议标记为答案 – 2014-09-10 20:25:12