将值传递给控制器

时间:2016-04-22 17:15:20

标签: javascript c# asp.net-mvc

我正在努力将所需的所有数据传递给MVC中的控制器。代码如下:

 <td><a href=@Url.Action("AddCourseToStudentPlan", "Users", new { 
courseID = c.CourseId, userID = user.UserId, semNum = num}) title="Add Course 
to Semester" >"Here"</a></td>

(目前num定义为var num = 1;,这当然不会起作用。)

课程ID和用户ID正在通过。这些值本质上来自ViewBag

但是,semNum取决于完全不同的东西:

<ul class="nav nav-tabs" id="Semtabs">
                    @{ var year = plan.CatalogYear; }
                    <li class="active"><a href="#SEM1" onclick="StoreNum(1)" data-toggle="tab" aria-expanded="true">Fall @year</a></li>
                    @{ year += 1; }
                    <li class=""><a href="#SEM2" data-toggle="tab" aria-expanded="true">Spring @year</a></li>
                    <li class=""><a href="#SEM3" data-toggle="tab" aria-expanded="true">Fall 2014</a></li>
                    <li class=""><a href="#SEM4" data-toggle="tab" aria-expanded="true">Spring 2015</a></li>
                    <li class=""><a href="#SEM5" data-toggle="tab" aria-expanded="true">Fall 2015</a></li>
                    <li class=""><a href="#SEM6" data-toggle="tab" aria-expanded="true">Spring 2016</a></li>
                    <li class=""><a href="#SEM7" data-toggle="tab" aria-expanded="true">Fall 2016</a></li>
                    ...

预期的输出是当用户点击其中一个标签时,他们会看到他们添加的课程列表,并且可以通过单击代码前一个代码块中的链接向该标签添加课程。因此,单击选项卡应更改num方法中@Url.Action()的值。但当然我无法做到这一点,因为C#是服务器端。

所以问题是,如何在页面加载后将一些变量semNum(确定客户端)传递给@Url.Action()方法?

1 个答案:

答案 0 :(得分:0)

有多种方法可以做到这一点。这个非常直观,不会在Visual Studio中导致语法错误。

  1. 使用您知道的信息创建一个网址模板。
  2. 单击链接时,请查看选项卡的当前状态。
  3. 使用该信息填写网址。
  4. 根据需要导航。
  5. <!-- define a link/button, but don't set the URL -->
    <a id="add-url-button">Add Course to Semester</a>
    
    <!-- your existing tabs -->
    <ul class="nav nav-tabs" id="Semtabs">
        <li><a href="#SEM2" data-semester-number="2">Tab 1</a></li>
        <li class="active"><a href="#SEM3" data-semester-number="3">Tab 2</a></li>
    
        <!-- etc. -->
    </ul>
    
    <script>
    
    // Create a URL template    
    
    // It sounds like semNum is the only value that needs to vary,
    // but you can create a template with multiple placeholders.
    
    // We pass zero for semNum so that routes will still match without
    // conflicting with a real semester number.
    var addUrlTemplate = '@Url.Action("AddCourseToStudentPlan", "Users", new { courseID = c.CourseId, userID = user.UserId, semNum = 0 }';
    
    // jQuery here for brevity (will work fine with vanilla JS)
    
    var addButton = $("#add-url-button"),
        tabs = $("#Semtabs");
    
    addButton.click(function(){
    
        // determine the selected tab by interrogating the list of tabs
        var activeTab = tabs.find("li.active"),
            semesterNumber = activeTab.attr("semester-numer");
    
        // Construct the complete URL. Depending on the URL, you may need
        // a smarter regex. Be sure that there is no chance of accidentally
        // replacing the wrong value.
        var addUrl = addUrlTemplate.replace(/0$/, semesterNumber);
    
        // navigate as desired
        window.location.href = addUrl;
    ));
    
    </script>