使用MVC从客户端将值分配给ViewData [英] Assign values to ViewData from client using MVC

查看:31
本文介绍了使用MVC从客户端将值分配给ViewData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了MVC应用程序,并且它是来自ASP.NET环境的真正的新手.我的问题是我无法在局部视图和控制器之间移动变量数据.在ASP.NET中,这很简单,而在MVC中则不是这样.因此,当从客户端选择Tab时触发以下代码,但我需要从客户端捕获selectedTabIndex值并将其传递给控制器​​.

I inherited an MVC app and am a true novice at it coming from an ASP.NET environment. My problem is I can't manage to move variable data between my partial views and controllers. In ASP.NET this was pretty straight forward, not the case in MVC. So the following code fires when a Tab is selected from the client but I need to capture the selectedTabIndex value from the client and pass it to the controller.

    function onTabSelect(e) {
    var selectedTabIndex = 0;
    selectedTabIndex = $(e.item).index();
    alert(selectedTabIndex);
    }

我考虑过使用ViewData对象,但是似乎没有一种方法可以从函数内部执行分配任务.因此,以下代码似乎是一个荒谬的任务,并且无论如何都会失败.(请记住,我是这个方面的新手)

I considered using the ViewData object but there doesn't appear to be a way to make perform this task of assignment from within the function. So the following code would seem to be a ridiculous task and it fails anyway. (Remember, I am an extreme novice at this)

    function onTabSelect(e) {
    var selectedTabIndex = 0;
    <% =ViewData["selectedTabIndex"]%> = selectedTabIndex;      
    }

我也考虑过使用cookie,但这似乎也不是一种实用的方法.在ASP.NET中,我可以使用.find方法访问客户端控件,但这对我来说是一个陡峭的学习曲线.

I also considered using cookies but that doesn't seem to be a practical method either. In ASP.NET I could access the client controls using the .find method but this is a steep learning curve for me.

我有什么选择?

推荐答案

如果用户可以在保存"操作之间选择不同的选项卡,则可能需要考虑绑定某种单击功能(使用jQuery)来设置 selectedTabIndex JavaScript变量.或者,您可以将隐藏输入的值设置为选定的选项卡索引.

If different tabs can be selected by the user between Save operations, you might need to consider binding some kind of click function (using jQuery) to set the selectedTabIndex javascript variable. Alternatively, you could set a hidden input's value to the selected tab index.

在任何一种情况下,如果在提交保存操作时需要控制器中的值(以设置ViewData,ViewBag,某些模型数据等),则可以通过 $.ajax 并从控制器返回一些JSON

In either case, if you need the value in your controller (to set ViewData, ViewBag, some model data, etc) when a Save operation is submitted, you could submit the data via $.ajax and return some JSON from your controller

$('#SaveButton').click(function() {
  $.ajax({
    url: '/Controller/Save',
    type: 'POST',
    // you might need to serialize additional data you want to save here
    // but you could create any JSON object before calling $.ajax
    data: {"selectedTabIndex": selectedTabIndex}, // data to POST
    dataType: 'json', // this indicates the type of data returned from controller
    success: function(data) {
      // you didn't really describe how to programatically set a tab,
      // so "setToTab" is a guess
      $('#tabID').setToTab(data.selectedTabIndex);
    }
  });
}

您的控制器可能看起来像

And your controller might look something like

public ActionResult Save(int selectedTabIndex)
{
  // again, you might need different parameters to complete your Save
  return JsonResult(new { selectedTabIndex: selectedTabIndex });
}

这篇关于使用MVC从客户端将值分配给ViewData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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