简体   繁体   English

如何将数据从控制器动作传递到局部视图…?

[英]How to pass the data from controller action to partial view…?

I am new to MVC and working on MVC 4 with API services and got stuck at the passing of data to partial view which is modal popup,and i need to get all those data on clicking on the products column on modal popup but its not hapening exactly.I have passed the data ie productid to the controller though knockout js its coming but not visible on modal popup. 我是MVC的新手,正在使用API​​服务进行MVC 4的开发,被卡在将数据传递给部分视图(即模式弹出窗口)的过程中卡住了,我需要在模式弹出窗口上的产品列上单击时获取所有这些数据,但它不会发生我已经通过敲除js将数据即productid传递给了控制器,但在模式弹出窗口中却不可见。 Here is my js code: 这是我的js代码:

    var ProductViewModel = function () {
        var self = this;
        $.ajax({
        url: urlProduct + '/AllProducts/',
        async: false,
       dataType: 'json',
        success: function (json) {
            self.productsData = json;
        }
    });
    self.getModalProductInfo = function (product) {
        alert("Get Product Info - for ID:" + product.ProductId);       
        var self = this;
        $.ajax({
            url: urlProduct + '/Modalpopup/' + product.ProductId,
            //url : '@Url.Action("/Modalpopup/", "Products")'+=product.ProductId,
            //url += '/?min=' + ui.values[0] + '&max=' + ui.values[1];
            //$("#ajaxresult").load(url),
            //'@{Html.RenderAction("Modalpopup", "Products",new {id=product.ProductId});}'               
            async: false,
            dataType: 'json',
            success: function (json) {
                self.modalPopupdata = json;

           }
        });
      } 

}

ko.applyBindings(new ProductViewModel());

And is my products page: 而且是我的产品页面:

    <div class="span3" style="margin-right:-1em;">
             <ul class="thumbnails" style="height:280px;">
                   <li >

                     <div class="thumbnail zoom" id="wrapper">

               <a href="#portfolioMod1"  data-toggle="modal" data-bind="click:$parent.getModalProductInfo">
                 <div data-bind="foreach:ProductImages"> 
                 <!-- ko if: SortOrder === 1-->
                    <img data-bind="attr:{src:Product_Image_path}" />
                 <!-- /ko -->      
                 </div>                      
                </a>


                <!-- Start: Modal -->               
                @Html.Partial("_ProductModalPopup")
                <!-- End: Modal -->

<div id="SL13" style="margin-bottom:0px; border-bottom:1px solid #dedede; height:20px">3 colors</div>  

<div class="text" style="display:none;"><img src="~/Images/thumb1.gif" /><img src="~/Images/thumb2.gif" />
<img src="~/Images/thumb3.gif" /><img src="~/Images/arrow.gif" align="right"/></div>



 <div style="margin-bottom: -3px;" data-bind="text:ProductName" ></div>
 <div ng-controller="RatingDemoCtrl"><rating value="rate" max="5" readonly="isReadonly"></rating></div>
    <div style="font-weight: lighter;margin-bottom: 5px;" data-bind="foreach:ProductInfo" >
                <!-- ko if: Key === 'Price'-->
                   <span style="color:#fe3c3e;" data-bind="text:Value"></span>
                 <!-- /ko -->    
 </div>

<div class="text" id="SD1" >
<button type="submit" class="btn btn-inverse btn-small" onclick="redirect13();" >
<small style=" font-size:8px; "><strong>ADD TO BAG</strong>
</small>
</button> 
<span style="float:right" align="top"><button type="submit" class="btn    btn-danger btn-small" onclick="redirect12();" >
<small style=" font-size:8px; ">CHECK OUT</small>
</button>
</span>
</div>
<div data-bind="text:ProductId" style="display:none"><div>
    <input id="pid" data-bind="text:ProductId" />
                               </div>
                       </li>
                 </ul>     
            </div>          
</div>

and where @Html.Partial("_ProductModalPopup") is the partial view. @ Html.Partial(“ _ ProductModalPopup”)是局部视图。 The controller action action is: 控制器动作为:

  public ActionResult Modalpopup( int id)
        {
            ModalPopup modalid = new ModalPopup();
            modalid.Productid = id;
            ViewBag.Pid = id;
            ViewBag.ModalPopup = modalid.Productid;
            ViewBag.Message = "The ProductID:";
            return PartialView("_ProductModalPopup",id);
       // return RestJsonCRUD.InvokeReadCall(URL + "/ProductRest/GetProductDetailsByID/" + id, null);
        }

Can any one help me in sorting the problems from the above code . 谁能帮我解决以上代码中的问题。

What you're trying to do isn't going to work because the partial view is rendered before the knockout bindings are applied, but you want to pass knockout data to the partial view. 您要执行的操作将无法正常工作,因为在应用敲除绑定之前已渲染了部分视图,但是您希望将敲除数据传递给部分视图。

The flow you should follow is this: 您应遵循的流程是这样的:

  1. Render Main Page with placeholder for partial view result 使用占位符渲染主页面以获得部分视图结果
  2. Create ko viewModel and applyBindings 创建ko viewModel和applyBindings
  3. ajax call for to Action that returns a PartialView ajax要求Action返回PartialView
  4. Place the result in the placeholder 将结果放置在占位符中

This means that your main page shouldn't have any @Html.Partial() referring to the partial view you want to render, because that's rendered before knockout can run. 这意味着您的主页不应包含任何@Html.Partial()引用要呈现的局部视图,因为该视图是敲除操作运行之前呈现的。

<!-- Start: Modal -->               
  <div id="modalPlaceHolder"></div>
<!-- End: Modal -->

self.getModalProductInfo = function (product) {
    alert("Get Product Info - for ID:" + product.ProductId);       
    var self = this;
    $.ajax({
        url: urlProduct + '/Modalpopup/' + product.ProductId,
        type: 'GET', // you're getting html, not posting anything
        success: function (partialViewResult) {
            $('#modalPlaceholder').html(partialViewResult);

       }
    });
  } 

You can try like this, 你可以这样尝试

public ActionResult MyPartialView(int id)
{
ModalPopup pop=repository.GetModalPop(id);
return View(pop);
}

your partial view probably, 您的局部看法,

@{
Layout=null;
}
@model Project.Models.ModalPop
<h1>@Model.Name</h1>
...
...
...

and you can load this partial view using ajax calls like, 您可以使用ajax调用(例如,

$('#divPlaceholder').load('/Home/MyPartialView?id='+$('#txt').val());

like that you can send data to view from your controller action. 这样,您可以发送数据以从控制器操作中查看。 Hope this helps. 希望这可以帮助。

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

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