作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
情况:在我的 C#/MVC 4 解决方案中,我使用的 View 中包含部分 View 。该 View 是一个带有提交按钮的表单。部分 View 有一个隐藏的 div,但如果选中该复选框则可以显示。
问题:如果隐藏部分 View ,则提交工作正常。如果未隐藏部分 View ,则提交会导致页面无响应,如果等待 3 分钟左右,提交最终会按预期工作。
代码如下。预先感谢您的考虑。我是一名开发新手,因此欢迎所有意见、建议和批评。
代码:
型号
namespace MyModels
{
public class MainModel
{
public SelectListItem Things { get; set;}
public IEnumerable<OtherModel> MoreThings { get; set;}
}
}
查看 //命名为我的 View @model MyModels.MainModel @使用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>
部分 View
//named _MyPartialView
@model MyModels.MainModel
@using MyModels
@foreach (var moreThings in ViewBag.moreThings)
{
<div id="replacedDiv">
<label>
<input type="checkbox" id=@moreThings.id value=@moreThings.name />@moreThings.name </label>
</div>
}
Controller
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);
}
}
}
最佳答案
在你使用的ajax中:
$('#replacedDiv").html(result);
但是您的部分 View 包含 <div id="replacedDiv">
在循环中生成
将部分 View 代码替换为:
@foreach (var moreThings in ViewBag.moreThings)
{
<label>@moreThings.name </label>
<input type="checkbox" id=@moreThings.id value=@moreThings.name />
}
应该没问题
关于c# - MVC 4 部分 View 导致页面在提交时无响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25771146/
我目前有一个功能可以从远程 url 抓取 mp3 文件,并将其上传到 Amazon S3 存储桶。 该函数似乎工作正常,因为该文件出现在 S3 中,但是我担心在我的本地服务器上使用隧道 (ngrok)
我是一名优秀的程序员,十分优秀!