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

情况:在我的C#/ MVC 4解决方案中,我采用的视图带有部分视图。该视图是带有提交按钮的表单。局部视图的div已隐藏,但是如果选中此复选框,则可以显示该视图。

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

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

码:

模型

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

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

视图
    //命名为MyView
    @model MyModels.MainModel
    @使用MyModels
    @if(Model!= 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);
       }
    }
}

参考方案

在您的ajax中,您正在使用:

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

但是您的局部视图包含在循环中生成的<div id="replacedDiv">

将您的部分视图代码替换为:

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

而且应该没关系

将Web用户控件添加到页面时,asp按钮onclick不会触发 - javascript

我正在使用使用Visual Studio模板创建的Web表单应用程序。模板具有一个内容占位符,该占位符被访问的任何页面的内容替换。有问题的页面有几个服务器控件,例如文本框,标签和按钮。当我单击我的更新按钮时,它可以正常工作,这些值会回传并更新数据库。我想在所有子页面上创建通用的登录提示。我的导航栏(位于我的母版页中)具有引导程序设置。我在导航栏中添加了一个下…

Asp.net发送信息表单到页面 - c#

我正在尝试使用弹出窗口中的新信息更新旧页面。到目前为止,我尝试过将结果保存在会话中Session["Data"] = DLvrijecampingplaatsen.SelectedItem; 然后当它达到Page_Load时,将其重新加载回旧页面if (Session["Data"] != null) { LBkies…

与Mootools Scriptmanager Ajax Asp.net发生冲突? - c#

我正在尝试不同的方法来使这项工作成功,但是没有成功。我正在尝试将mootools与asp.net应用程序集成。我只想用它为我的网站添加一些效果。我也使用更新面板,scriptmanager,因为不希望有完整的回发。在控制台上引发错误TypeError:clientID.startsWith不是函数,并且在进行更新时brwoser reset...。这是整个代…

Div单击与单选按钮相同吗? - php

有没有一种方法可以使div上的click事件与表单环境中的单选按钮相同?我只希望下面的div提交值,单选按钮很丑代码输出如下:<input id="radio-2011-06-08" value="2011-06-08" type="radio" name="radio_date&#…

ASP.NET MVC 5自定义登录,无需脚手架,数据库优先 - c#

我对asp.net和mvc还是很陌生,所以我正在努力学习尽可能多的知识...为此,我从头开始编写博客网站,但是我对身份验证和授权有些困惑。由于我倾向于不真正使用任何脚手架的东西,所以我首先要使用数据库,所以不希望asp.net身份为我创建表。我对散列和加盐密码很酷,并对照数据库检查用户,我遇到的麻烦是将用户设置为登录状态并检查他们应该能够访问什么。我真的很想…