首页 \ 问答 \ 如何在Javascript代码中访问rails变量?(How to access rails variables within Javascript code?)

如何在Javascript代码中访问rails变量?(How to access rails variables within Javascript code?)

我正在使用Rails 3.0.5

我有一个使用jQuery下拉清单的javascript代码,我希望在我的物品控制器的“编辑”操作中使用我的表单时,显示哪些项目已被选中,哪些不是。

基本上,它可以在Javascript中像这样工作:

$('element').val([1,3,4]);
$('element').dropdownchecklist();

让我们说在我的控制器中,编辑动作,我有一个名为@selected_items的实例变量,它包含选中的项目(这里是@selected_items = [1,3,4]),我想使用这个值实例变量放入我的Javascript代码中,我该如何继续? 我怎么能在.js文件中访问这个实例变量的值?

非常感谢您的回答!


I am using Rails 3.0.5

I have a javascript code that uses jQuery dropdown checklist and I would like to have the checkboxes displaying which item is checked already and which is not, when using my form in the 'Edit' action of my belongings controller.

Basically, it can work like this in Javascript:

$('element').val([1,3,4]);
$('element').dropdownchecklist();

Let us say that in my controller, edit action, I have a instance variable called @selected_items that contains the items that are selected (here: @selected_items = [1,3,4]) and I would like to use the value of this instance variables into my Javascript code, how could I proceed ? How could I access the value of this instance variable in a .js file ?

Many thanks for your answer !


原文:https://stackoverflow.com/questions/12260104
更新时间:2021-08-24 12:08

最满意答案

这就是我最终做的。 它并不感觉完全干净,但我保留了最小方法的责任。 我想有一个更好的方式做这个没有TempData,如果任何人有更好的建议,请张贴你的例子。

    // Inital loaded view with 3 partial views
    // 2 for search criteria, 1 for results
    public ActionResult Index() {
        var model = new SearchUsersViewModel();
        if(TempData["model"] != null)
            model = (SearchUsersViewModel)TempData["model"];
        return View(model);
    }

    // Post the search criteria model for search 
    // by user data (by last name, etc...)
    // Then redirect back to Index.
    // TempData will have the full model with results populated
    [HttpPost]
    public ActionResult SearchByUser(FilterUsersByUserDataViewModel model) {
        if(ModelState.IsValid) {
            var list = SearchUserService.SearchByValue(model.LastName, 
                                                       model.Username,
                                                       model.EmailAddress);
            if(list != null) {
                TempData["model"] = PrepareResultsModel(list);
                return RedirectToAction("Index");
            }
        }
        return View(model);
    }

    // This method just separates the concern of creating 
    //the new full model with search results populating the results view
    private SearchUsersViewModel PrepareResultsModel(List<SearchUserViewDTO> list) {
        var searchResults = new UserSearchResultsViewModel();
        list.ForEach(item => searchResults.Users.Add(new UserViewModel(item)));
        var model = new SearchUsersViewModel();
        model.UserSearchResultsViewModel = searchResults;
        return model;
    }

This is what I ended up doing. It doesn't feel completely clean but I've kept the responsibilities of the methods minimal. I'd like to have a better way of doing this without TempData if anyone has a better suggestions please post your example.

    // Inital loaded view with 3 partial views
    // 2 for search criteria, 1 for results
    public ActionResult Index() {
        var model = new SearchUsersViewModel();
        if(TempData["model"] != null)
            model = (SearchUsersViewModel)TempData["model"];
        return View(model);
    }

    // Post the search criteria model for search 
    // by user data (by last name, etc...)
    // Then redirect back to Index.
    // TempData will have the full model with results populated
    [HttpPost]
    public ActionResult SearchByUser(FilterUsersByUserDataViewModel model) {
        if(ModelState.IsValid) {
            var list = SearchUserService.SearchByValue(model.LastName, 
                                                       model.Username,
                                                       model.EmailAddress);
            if(list != null) {
                TempData["model"] = PrepareResultsModel(list);
                return RedirectToAction("Index");
            }
        }
        return View(model);
    }

    // This method just separates the concern of creating 
    //the new full model with search results populating the results view
    private SearchUsersViewModel PrepareResultsModel(List<SearchUserViewDTO> list) {
        var searchResults = new UserSearchResultsViewModel();
        list.ForEach(item => searchResults.Users.Add(new UserViewModel(item)));
        var model = new SearchUsersViewModel();
        model.UserSearchResultsViewModel = searchResults;
        return model;
    }

相关问答

更多
  • 看看这个链接 - 如何使用AJAX在MVC中加载部分视图 这是另一个例子: Java脚本: $.get( '@Url.Action("PopulateTextBoxes","Home", new { customerId = Model.ID } )', function(data) { $('#outputContainer').html(data); }); 控制器: public ActionResult PopulateTextBoxes(Int32 customerId, string ...
  • 您需要将将加载对话框的脚本放在您将调用局部视图的视图上。 fullview.cshtml (示例视图)

    Some markup goes here