2016-04-22 177 views
1

我很努力将所需的所有数据传递给MVC中的控制器。代码如下:(目前NUM被定义为var num = 1;,这当然不会工作)将值传递给控制器​​

<td><a [email protected]("AddCourseToStudentPlan", "Users", new { 
courseID = c.CourseId, userID = user.UserId, semNum = num}) title="Add Course 
to Semester" >"Here"</a></td> 

课程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> 
        ... 

预期输出是当在其中一个选项卡的用户点击,他们看到的,他们已经加入课程的列表,并能通过点击代码之前代码块中的链接将课程添加到此选项卡。因此,单击选项卡应在@Url.Action()方法中更改num的值。但当然,我不能这样做,因为C#是服务器端。

所以问题是,我怎么能够在页面加载后将一些变量semNum(这是确定的客户端)传递给@Url.Action()方法?

+0

当用户点击该链接,您可以发布最终'num'值到你的服务器,并相应地重定向到该行动在控制器 – silkfire

+0

你的问题是不是真的我清楚。但无论如何,如果你的问题是Url.Action,那么你可以跳过使用它。只需使用一个java脚本函数,在函数中做你所需要的任何事情,然后重定向 –

回答

0

有多种方式可以做到这一点。这一点相当直观,不会导致Visual Studio中的语法错误。

  1. 使用您所知道的信息创建网址模板。
  2. 单击链接时,查看选项卡的当前状态。
  3. 使用该信息来完成URL。
  4. 根据需要进行导航。
<!-- 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> 
+0

我应该已经更清楚了,但是我很快就写下了它;道歉。 courseID也是可变的,因为我希望用户能够选择它。因此,以这种方式设置预制网址将无法使用。不过,我想我可能会修改此解决方案,以便JavaScript函数构建正确的URL。非常感谢! –