gpt4 book ai didi

c# - MVC 4 部分 View 导致页面在提交时无响应

转载 作者:行者123 更新时间:2023-12-02 17:00:28 24 4
gpt4 key购买 nike

情况:在我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com