从控制器MVC通模型对象查看 [英] MVC pass model object from controller to view

查看:141
本文介绍了从控制器MVC通模型对象查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出如何从控制器传递模型对象,查看在以下情形:

 <%Html.Action(的GetRequest,The_Controller,新{=的requestId 12}); %GT;
<%Html.RenderPartial(请求,ViewData.Model); %GT;

动作宜,它本身,调用控制器的方法,查找在DB库Request对象,并返回一个Request对象。

局部视图,命名请求,应接受请求对象并显示它。

的问题是,在ViewData.Model总是空?!

我试图弄明白,但没有成功:(

这样做的原因行为是我需要显示的基础上通过的jqGrid提供的RequestID值jQuery的模态对话框内的局部视图。我已经到了我打开对话框,但不能得到模型对象实例的地步。

任何帮助是AP preciated。

解决方案 - 与尼克·拉森和达林季米特洛夫的帮助

控制器:

 的[AcceptVerbs(HttpVerbs.Post)
    [授权]
    公共PartialViewResult的GetRequest(字符串的requestId)
    {
        请求请求= DatabaseContext.GetRequest(Convert.ToInt32(的requestId))的请求;
        返回PartialView(请求,请求);
    }

该视图的JavaScript:

 函数OpenRequest(的requestId){        变种方法名='&下;%= Url.Content(〜)%>' +'控制器/的GetRequest /';
        VAR的dataType =HTML;
        VAR POSTDATA = {的requestId:将requestId};
        VAR的contentType =应用/的X WWW的形式urlen codeD; ;
        VAR请求= ContactServer(方法名,POSTDATA,数据类型的contentType);        $(#对话)HTML(请求);
        $(#对话)对话框({HTML:要求,标题:请求详细信息...',宽度:800,高度:600,模态:真的,zIndex的:300000,显示:规模,调整大小:假的});    }


解决方案

这是更好地将它设置在您的观点并没有打电话给你的控制器。加载所有数据的对在其中要求该视图的动作请求并填充视图模型与所需要的数据。一旦你的,呈现从模型中的字段。

至于你的实际问题。调用摆在首位这一观点的动作填充其上下文ViewData.Model。当你调用操作方法,该框架是创建有自己的ViewData一个新的上下文,你不能够访问没有一个句柄新创建的上下文。

I am trying to figure out how to pass the model object from controller to view in a following scenario:

<% Html.Action("GetRequest", "The_Controller", new { requestId = 12 }); %>
<% Html.RenderPartial("Request", ViewData.Model); %>

The action should, and it does, call the controller's method which looks up the Request object in the DB repository and returns a Request object.

The partial view, named Request, should receive the request object and display it.

The problem is that the ViewData.Model is always null?!

I have tried to figure it out, but with no success :(

The reason for this behavior is that I need to display a partial view inside the jQuery's modal dialog based on the requestId value provided by the jQGrid. I have reached the point where I open the dialog but can't get that model object instance.

Any help is appreciated.

The solution - With the help of Nick Larsen and Darin Dimitrov

The controller:

    [AcceptVerbs(HttpVerbs.Post)]
    [Authorize]
    public PartialViewResult GetRequest(string requestId)
    {
        Request request = DatabaseContext.GetRequest(Convert.ToInt32(requestId)) as Request;
        return PartialView("Request", request);
    }

The view's Javascript:

function OpenRequest(requestId) {

        var methodName = '<%= Url.Content("~")%>' + 'Controller/GetRequest/';
        var dataType = "html";
        var postData = { requestId: requestId };
        var contentType = "application/x-www-form-urlencoded"; ;
        var request = ContactServer(methodName, postData, dataType, contentType);

        $("#dialog").html(request);
        $("#dialog").dialog({ html: request, title: 'Request details...', width: 800, height: 600, modal: true, zindex: 300000, show: 'scale', resizable: false });

    }

解决方案

It's better to set it up where your view does not call your controller. Load all of the data for the request in the action which calls this view and populate a view model with the needed data. Once you have that, render the fields from the model.

As for your actual problem. The action that calls this view in the first place populates the ViewData.Model for its context. When you call the action method, the framework is creating a new context with its own ViewData which you dont have access to without a handle to that newly created context.

这篇关于从控制器MVC通模型对象查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆