为什么我要下载一个 aspx 页面而不是在 MVC 2 中导航到它

发布于 2024-11-13 00:37:14 字数 2130 浏览 12 评论 0原文

这可能是堆栈溢出上最愚蠢的问题。但我从我正在使用的一些代码中得到了最奇怪的结果。我正在尝试让 jqGrid 在我的 MVC 2 应用程序中工作。

我的家庭控制器有一个针对 Index 和 GridData 的操作方法...GridData 有 4 个参数,其中 2 个不能为空,因此我向它们添加了一个值为 1 的 defalutValue 属性。 Index 控制器重定向到 GridData 操作方法,然后打开一个 GridData 视图...我不返回此函数中的视图,但返回一个 Json 变量...

[Authorize(Roles="testRole")]
    public ActionResult Index(string nextButton)
    {
        ViewData["identity_Name"] = identity.Name;
        if (nextButton != null)
            return RedirectToAction("GridData");
        return View("Index");
    }

    public ViewResult windowsID()
    {
        return View();
    }

    public ActionResult GridData(string sidx, string sord, [DefaultValue(1)] int page, [DefaultValue(1)] int rows) 
    {
        var jsonData = new 
        {
            total = 1, // we'll implement later 
            page = page,
            records = 3, // implement later 
            rows = new[]    
            {
                new {id = 1, cell = new[] {"1", "-7", "Is this a good question?"}},
                new {id = 2, cell = new[] {"2", "15", "Is this a blatant ripoff?"}},
                new {id = 3, cell = new[] {"3", "23", "Why is the sky blue?"}}
            }   
        };
        return Json(jsonData, JsonRequestBehavior.AllowGet);
    }
}

这是我的大部分 Javascript 代码。

<script type="text/javascript">
jQuery(document).ready(function () {
    jQuery("#list").jqGrid({
        url: '/Home/GridData/',
        datatype: 'json',
        mtype: 'GET',
        colNames: ['Id', 'Votes', 'Title'],
        colModel: [
      { name: 'Id', index: 'Id', width: 40, align: 'left' },
      { name: 'Votes', index: 'Votes', width: 40, align: 'left' },
      { name: 'Title', index: 'Title', width: 200, align: 'left'}],
        pager: jQuery('#pager'),
        rowNum: 10,
        rowList: [5, 10, 20, 50],
        sortname: 'Id',
        sortorder: "desc",
        viewrecords: true,
        imgpath: '/scripts/themes/smoothness/images',
        caption: 'My first grid'
    });
}); 

看起来很合理吧?为什么它会下载该页面而不是重定向到该页面?我到底可能做错了什么。我想很多,但我想我只是错过了一些简单的东西。

This is probably the dumbest question ever here on stack Overflow. But I am getting the weirdest results from some code that I am working with. I am trying to get jqGrid to work in my MVC 2 application.

My home controller has an action method for Index and GridData... GridData takes 4 parameters, 2 of them cannot be null so I add a defalutValue attribute with a value of one to them. The Index controller redirects to the GridData action method wich then opens up a GridData view... I don't return the View in this function but I return a Json variable...

[Authorize(Roles="testRole")]
    public ActionResult Index(string nextButton)
    {
        ViewData["identity_Name"] = identity.Name;
        if (nextButton != null)
            return RedirectToAction("GridData");
        return View("Index");
    }

    public ViewResult windowsID()
    {
        return View();
    }

    public ActionResult GridData(string sidx, string sord, [DefaultValue(1)] int page, [DefaultValue(1)] int rows) 
    {
        var jsonData = new 
        {
            total = 1, // we'll implement later 
            page = page,
            records = 3, // implement later 
            rows = new[]    
            {
                new {id = 1, cell = new[] {"1", "-7", "Is this a good question?"}},
                new {id = 2, cell = new[] {"2", "15", "Is this a blatant ripoff?"}},
                new {id = 3, cell = new[] {"3", "23", "Why is the sky blue?"}}
            }   
        };
        return Json(jsonData, JsonRequestBehavior.AllowGet);
    }
}

Here is most of my Javascript code.

<script type="text/javascript">
jQuery(document).ready(function () {
    jQuery("#list").jqGrid({
        url: '/Home/GridData/',
        datatype: 'json',
        mtype: 'GET',
        colNames: ['Id', 'Votes', 'Title'],
        colModel: [
      { name: 'Id', index: 'Id', width: 40, align: 'left' },
      { name: 'Votes', index: 'Votes', width: 40, align: 'left' },
      { name: 'Title', index: 'Title', width: 200, align: 'left'}],
        pager: jQuery('#pager'),
        rowNum: 10,
        rowList: [5, 10, 20, 50],
        sortname: 'Id',
        sortorder: "desc",
        viewrecords: true,
        imgpath: '/scripts/themes/smoothness/images',
        caption: 'My first grid'
    });
}); 

Seems reasonable right? Why on earth would it download the page instead of redirecting to it? What on Earth could I possible be doing wrong here. Well plenty I guess, but I think I am just missing something simple.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

垂暮老矣 2024-11-20 00:37:14

好的。我想我明白了。我不想重定向到某个操作。该操作返回 Json 数据类型,这只是文本,因此浏览器将尝试下载它。我想重定向到一个视图...所以我所做的是创建一个返回 json 数据的辅助函数,然后我重定向到 dataGrid 控制器。但是 Json 脚本现在调用辅助函数并且有效...除了我知道如何解决的 Javascript 错误(它基本上是一个格式错误,因此如果我单击它,它将呈现)。

不管怎样,谢谢你们的帮助。

德里克

P.S 这是我的编码解决方案,如果您遇到同样的问题,您想亲自检查一下......
jqGrid 调用

jQuery(document).ready(function () {
    jQuery("#list").jqGrid({
        url: '/Home/GetData/',
        datatype: 'json',

这是我在控制器内的操作。

 public JsonResult GetData(string sidx, string sord, [DefaultValue(1)] int page, [DefaultValue(3)] int row)
    {
        var jsonData = new
        {
            total = 1, // we'll implement later 
            page = page,
            records = 3, // implement later 
            rows = new[]    
            {
                new {id = 1, cell = new[] {"1", "-7", "Is this a good question?"}},
                new {id = 2, cell = new[] {"2", "15", "Is this a blatant ripoff?"}},
                new {id = 3, cell = new[] {"3", "23", "Why is the sky blue?"}}
            }
        };
        return Json(jsonData, JsonRequestBehavior.AllowGet);
    }

    public ActionResult GridData() 
    {
        return View("GridData");
    }

正如您所看到的,在我重定向到 GridData()(它调用 griddata 视图)后,jqGrid 然后调用 GetData()。 GetData 然后返回在视图中呈现的 Json 数据...

如果有人有更好的方法来做到这一点,请回复。但感谢您的帮助。

德里克

Ok. I think I figured it out. I don't want to redirect to an action. The action returns a Json data type and that is just text so the browser is just going to attempt to download it. I want to redirect to a view... So what I did is make a helper function that returns a json data and I do redirect to the dataGrid controller. But the Json script now calls the helper function and that worked... Except for a Javascript error which I have know Idea how to solve (It is basically a formatting error so if I click past it, it will render).

Thanks for the help anyway guys.

Derek

P.S here is my coded solution incase you wanted to check it out for yourself if you are having the same problem...
jqGrid call

jQuery(document).ready(function () {
    jQuery("#list").jqGrid({
        url: '/Home/GetData/',
        datatype: 'json',

Here are my actions inside the controller.

 public JsonResult GetData(string sidx, string sord, [DefaultValue(1)] int page, [DefaultValue(3)] int row)
    {
        var jsonData = new
        {
            total = 1, // we'll implement later 
            page = page,
            records = 3, // implement later 
            rows = new[]    
            {
                new {id = 1, cell = new[] {"1", "-7", "Is this a good question?"}},
                new {id = 2, cell = new[] {"2", "15", "Is this a blatant ripoff?"}},
                new {id = 3, cell = new[] {"3", "23", "Why is the sky blue?"}}
            }
        };
        return Json(jsonData, JsonRequestBehavior.AllowGet);
    }

    public ActionResult GridData() 
    {
        return View("GridData");
    }

As you can see after I redirect to GridData(), which calls the griddata view, jqGrid then calls GetData(). GetData then returns Json data which is rendered in the view...

If anyone has a better way to do this, please reply. But thanks for the help.

Derek

秋凉 2024-11-20 00:37:14

我认为您的问题就在这里:

if (nextButton != null)
            return RedirectToAction("GridData");

可能您正在尝试将视图重定向到实际返回 JSON 结果的操作。

尝试快速眼动这些线条,看看会发生什么。

更新:

在我看来,您误解了 MVC 的工作方式。
要解决您的问题,您可以尝试清理您的操作视图,使其看起来像这样:

public ActionResult Index()
    {
        return View();
    }

然后您需要有关联的视图,其中必须包含您的 jqGrid。
然后你需要有一个简单地返回一些 json 数据的操作。
该操作将始终由您的 jqGrid 调用(并且仅由您的 jqGrid 调用)。
这正是 GridData 的作用。
您不会重定向到 GridData,因为它是从 jqGrid 调用的。

Craig Stuntz 对此有一个很好的教程
这个一个也可能有帮助。

您可以在此处下载我的示例代码。

I reckon your problem is here:

if (nextButton != null)
            return RedirectToAction("GridData");

Probably you're trying to redirect your view to the action which actually returns a JSON result.

Try to REM those lines and see what happens.

UPDATE:

It seems to me that you're misunderstanding the way that MVC works.
To solve your problem you can try to clean your action View so it looks like this:

public ActionResult Index()
    {
        return View();
    }

Then you need to have the associated view, which must contain your jqGrid.
Then you need to have an action which simply returns some json data.
That action will always be called (and only) by your jqGrid.
That's exactly what your GridData does.
You do not redirect to GridData cause that is called from jqGrid.

Craig Stuntz has got a good tutorial about that.
This one might help too.

You can download my sample code here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文