简体   繁体   English

mvc PartialView with Dialog Partial View显示html

[英]mvc PartialView with Dialog Partial View showing html

What I am trying to do is to open up a jquery dialog. 我想要做的是打开一个jquery对话框。 What is happening is that I see the following html text vs the rendering of the form when it tries to open up the PartialView: 发生的事情是,当我尝试打开PartialView时,我看到以下html文本与表单的呈现:

     <form action="/Plt/FileUpload" method="post"><input data-val="true" data-val-number="The field PlNum must be a number." data-val-required="The PlNum field is required." id="PlNum" name="PlNum" type="hidden" value="36028" />     <div id="errMsg" >

     </div>
     <p>File upload for Pl# 36028</p>
     <input type="file" name="file" />
     <input type="submit" value="OK" />
     </form>

Here is the controller action: 这是控制器动作:

       public ActionResult FileUpload(int id)
       {
         var model = new FileUpload { PlNum = id };
         return PartialView(model);
       }

This is what the view looks like for the PartialView: 这是PartialView的视图:

    @model Ph.Domain.Lb.Models.FileUpload

    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    @using (Html.BeginForm("FileUpload", "Plts", FormMethod.Post, null))
    {

      @Html.HiddenFor(a => a.PlNum)

     <div id="errMsg" >
     @if (TempData["ErrMessage"] != null)
     {
       @TempData["ErrMessage"]
     }
     </div>

    <p>File upload for Pl# @Model.PlNum</p>
    <input type="file" name="file" />
    <input type="submit" value="OK" />
   }

This is what my ajax call looks like: 这是我的ajax调用的样子:

      var url = '@Url.Action("FileUpload", "Plt")' + '?id=' +  encodeURIComponent(rowid);

                 $.ajax({
                           url: url,
                           type: 'GET',
                           success: function(result) {                                    

                if (result.success) {
                  $('#dialog').dialog('close');
                } else {

             // refresh the dialog
                $('#dialog').html(result);
             }
         }  

To recap, the ajax call does reach the ActionResult but not sure when it tries to show the partial view it shows HTML vs the rendered html. 回顾一下,ajax调用确实到达ActionResult,但不确定它何时尝试显示部分视图,它显示HTML与渲染的html。

The issue here is that you are trying to load razor view which has not been rendered into the dialog's innerHTML. 这里的问题是你正在尝试加载尚未渲染到对话框的innerHTML中的剃刀视图。 Instead what you should be doing is setting href property of the dialog to the URL.Action link, when creating the dialog. 相反,您应该做的是在创建对话框时将对话框的href属性设置为URL.Action链接。 See the link below for an example. 请参阅以下链接以获取示例。

http://www.matthidinger.com/archive/2011/02/22/Progressive-enhancement-tutorial-with-ASP-NET-MVC-3-and-jQuery.aspx http://www.matthidinger.com/archive/2011/02/22/Progressive-enhancement-tutorial-with-ASP-NET-MVC-3-and-jQuery.aspx

The other option, which is not very maintainable IMO, but which will work with way you are currently doing, is to return the raw HTML from the action method. 另一个选项,它不是可维护的IMO,但它将以您当前的方式工作,是从操作方法返回原始HTML。

I think the first solution is better because the controller is not polluted with HTML string concatenation. 我认为第一种解决方案更好,因为控制器没有被HTML字符串连接污染。

jQuery won't let you use a script inside .html() . jQuery不允许你在.html()使用脚本。 You can do this by two ways: 你可以通过两种方式做到这一点:

Native DOM HTML injection instead: 相反,原生DOM HTML注入:

$('#dialog')[0].innerHTML = result;

.

Or, setting it as a data attribute and loading it manually: 或者,将其设置为数据属性并手动加载:

In view: 在视图中:

     <form action="/Plt/FileUpload" ...
         data-script="@Url.Content("~/Scripts/jquery.validate.min.js")"
           ... />

In JS: 在JS中:

$('#dialog').html(result);
var dialogScript = $('#dialog').children().first().data("script");
if(!!dialogScript) { $.getScript(dialogScript); };

Reference: http://api.jquery.com/jQuery.getScript/ 参考: http//api.jquery.com/jQuery.getScript/

.

Another way is use the load method, as in: 另一种方法是使用load方法,如:

$("#dialog").load(url, null, function() {
    // on a side note, put $("#dialog") in a variable and reuse it
   $("#dialog").dialog();
});

Reference: http://api.jquery.com/load/ 参考: http//api.jquery.com/load/

.

In the very case of jQuery validation, I'd consider adding it to the parent page itself. 在jQuery验证的情况下,我会考虑将其添加到父页面本身。 You'd expect it to be used in fair number of situations. 您希望它可以在相当多的情况下使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM