c# - 来自布局页面中的部分 View 。微 Controller 5

标签 c# model-view-controller

我的布局页面中有一个部分 View ,其中包含一个表单。部分 View 以 popup 形式打开。提交表单后,我必须返回到当前页面并打开弹出窗口,并且那里应该会出现一条成功消息,但我无法返回到正确的 View 。

我的布局页面

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>@ViewBag.Title || Midas WebSite</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/jqueryui")
    @Scripts.Render("~/bundles/site")
    @Scripts.Render("~/bundles/modernizr")

    <script type="text/javascript" language="javascript">
        /* Superior Web Systems */
        function displayHideBox(boxNumber) {

            if (document.getElementById("LightBox" + boxNumber).style.display == "none") {
                $("#LightBox" + boxNumber).fadeIn();
                $("#grayBG").fadeIn();
            } else {
                $("#LightBox" + boxNumber).fadeOut();
                $("#grayBG").fadeOut();
            }
        }
    </script>

</head>
<body>
    <div id="grayBG" class="grayBox" style="display:none;"></div>
    <div id="LightBox1" class="box_content" style="display:none;">
        <div onclick="displayHideBox('1'); return false;" class="close">X</div>
     @Html.Action("ActionToBindPartialView", "ControllerName")
</div>

 @RenderBody()
 @RenderSection("scripts", required: false)
 @Scripts.Render("~/bundles/jqueryval")

我的部分 View

<div id="divActions">
   // My Form Here

    @Ajax.ActionLink("Submit", "ActionName", "ControllerName", new AjaxOptions
         {
           HttpMethod = "POST",
           UpdateTargetId = "divActions",
           OnSuccess = "onSucess()" 
         })

</div>

现在是我迄今为止尝试过的操作代码:

1.

// My Code

 return PartialView("MyPartialView");

2

// My Code

return PartialView("MyPartialView", MyModel);

3

// My Code

return View();

4

// My Code

return ParialView();

我是一个初学者,无法找到我做错了什么。请建议我应该做什么才能使这项工作成功。

最佳答案

很难说你做错了什么,由于我们必须讨论这个问题的工作空间有限。不过,这里有一个可能对您有帮助的工作示例,请按照每个步骤操作:

  1. bundle

    bundles.Add(new ScriptBundle("~/bundles/jquery")
        .Include("~/Scripts/jquery-{version}.js"));
    
    bundles.Add(new ScriptBundle("~/bundles/jqueryval")
         .Include(
                    "~/Scripts/jquery.unobtrusive*",
                    "~/Scripts/jquery.validate*"));
    
  2. 家庭 Controller

    public ActionResult Index()
    {
        return View();
    }
    
    public PartialViewResult GetPartial()
    {
        return PartialView("_MyPartial");
    }
    
  3. 索引.cshtml

    <h2>Index</h2>
    
    <div id="divActions">
        @Ajax.ActionLink("Submit", "GetPartial", "Home", new AjaxOptions
         {
           HttpMethod = "POST",
           UpdateTargetId = "divActions",
           OnSuccess="onSuccess()"
         })
    </div>
    
    <script type="text/javascript">
        function onSuccess()
        {
            alert("Success!");
        }
    </script>
    
  4. _MyPartial.cshtml

     <h2>_MyPartial</h2>
    
  5. _Layout.cshtml

    <!DOCTYPE html>
    <html>
    <head>
       <title>@ViewBag.Title</title>
        @Styles.Render("~/Content/css")
        @Scripts.Render("~/bundles/modernizr")
    </head>
    <body>
        @RenderBody()
    
        @Scripts.Render("~/bundles/jquery")
        @Scripts.Render("~/bundles/jqueryval")
        @RenderSection("scripts", required: false)
    </body>
    </html>
    
  6. 要确认页面源中呈现的脚本(在浏览器中):

     <script src="/Scripts/jquery-1.8.2.js"></script>
     <script src="/Scripts/jquery.unobtrusive-ajax.js"></script>
     <script src="/Scripts/jquery.validate.js"></script>
     <script src="/Scripts/jquery.validate.unobtrusive.js"></script>
    

如您所见,这没有什么难的。您只需按照上述步骤操作即可。

希望对您有帮助。

提示:检查其他脚本错误,可能是您显示弹出窗口的自定义代码导致了错误。仔细检查您是否在局部 View 中渲染 javascript。考虑将其提取到常规 View 。

关于c# - 来自布局页面中的部分 View 。微 Controller 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23994571/

相关文章:

c# - 如何判断当前应用是否为Medium Trust

c# - FindObjectOfType 问题

PHP - 在 MVC 中在哪里实现 session 逻辑?

java - 使用 servlet 登录,而不是验证->重定向方式

c# - Okta SDK 过滤具有活跃状态并使用 StartsWith 和 Contains 关键字的用户

c# - 在asp.net 中如何确定用户来自哪里?

foreach 中的 C# 语法不明确

c# - 为模型类c#创建元数据类和分部类有什么用

c# - 委托(delegate),我不明白的事件约定

model-view-controller - 在 JSF2 托管 bean 中实现 MVC 的最佳实践