情況:在我的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);
}
}
}
你在你的JavaScript有語法錯誤。雖然這可能不會導致這個具體問題,但它肯定沒有幫助。 – David 2014-09-10 17:08:42
刪除代碼中的某些元素以找出根本原因。 – Rohit 2014-09-10 17:09:50
我剛更新的JavaScript,這是一個錯字,而不是在實際的代碼 – 2014-09-10 17:12:48