2014-02-12 54 views
0

我有一個MVC視圖,其中登錄用戶選擇課程(我有下拉列表)並單擊添加按鈕。我的控制器獲取登錄的人員ID,然後進行添加。我無法弄清楚如何將選定的courseId傳遞給控制器​​中的post方法。將參數傳遞給按鈕單擊的方法

我有類似:

[HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult ChooseCoursePreferences() 
    { 
     ViewBag.CourseId = new SelectList(db.Courses, "CourseId", "Name"); 
     int personid = repository.GetLoggedInPersonId(); 
     int courseid = ???; 
     repository.AddCoursePreferences(personid, courseid); 
     return View(); 
    } 

視圖有:

@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 
    <table class="table"> 
     <tr> 
      <td>PLease choose a course:</td> 
      <td>@Html.DropDownList("CourseId", String.Empty)</td> 
      @Html.ActionLink("Add", "ChooseCoursePreferences", new { ??? }) 
      <tr></tr> 
    </table> 
} 

另外,如果我想用一個按鈕點擊什麼。

我試過這個:但是我又如何得到我選擇的courseId? 添加課程

謝謝!

回答

1

我完全同意revdrjrr,熟悉mvc,你在做什麼是非常基本的。但我會在這裏幫助你。

​​

查看

@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 
    <table class="table"> 
     <tr> 
      <td>PLease choose a course:</td> 
      //i will need more information to be able to help you out here    
      //I am not quite sure where this information should come from 
      <td>@Html.DropDownList("CourseId", String.Empty)</td> 
      <input type="submit"/> 
      <tr></tr> 
    </table> 
} 
+0

酷感謝。我一直在太快地學習這些東西。這是一場掙扎,儘管如此。謝謝你的幫助! – SKale