2012-07-18 53 views
1

我有這個問題。我需要在控制器方法期間訪問用戶通過語句輸入的數據。訪問MVC控制器中的HTML表單數據

這裏是我的代碼,也許這將使它更加明確:

// client side 
@using (Html.BeginForm()) 
{ 
    if (competitorList.Count > 0 && eventList.Count > 0) 
    { 
     foreach (Event evt in eventList) 
     { 
      <table> 
      <tr><th>@evt.activity.Name</th></tr> 
      <tr> 
      <th>Name</th> 
      <th>Email</th> 
      <th>Score</th> 
      <th>New Score</th> 
      <th>Update</th> 
      </tr> 
      @foreach (Results res in resultList) 
      { 
       if (res.EventID == evt.id) 
       { 
        string competitorName = Person.getUserByEmail(res.CompetitorEmail).FirstName + Person.getUserByEmail(res.CompetitorEmail).LastName; 


        <tr> 
         <td>@competitorName</td> 
         <td>@res.CompetitorEmail</td> 
         <td>@res.Score</td> 
         <td><form action="EventResults"><input type="text" name="score" id="score" /></form></td> 
         <td>@Html.ActionLink("Update", "UpdateResults", "Competition", new { compId = evt.competitionId, evtId = res.EventID, email = res.CompetitorEmail }, null)</td> 
        </tr> 
       } 
      } 
      </table> 
      <hr /> 
     } 
    } 
    else 
    { 
     <p>There are currently no competitors invited to participate</p> 
    } 
} 


// controller 
public ActionResult UpdateResults(FormCollection form, int compId, int evtId, string email) 
     { 
      //////  this returns 0.0  ///// 
      double score = Convert.ToDouble(form["score"]); 

      BINC.Models.Results.UpdateResults(evtId, email, score); 

      List<Event> CompetitionEvents = Event.getEventsByCompetitionId(compId); 
      ViewBag.CompetitionEvents = CompetitionEvents; 

      List<Competitor> Competitors = Competitor.getCompetitors(compId); 
      ViewBag.Competitors = Competitors; 

      List<Results> Results = Competition.getCompetitorResultsPairings(CompetitionEvents, Competitors); 
      ViewBag.Results = Results; 

      ViewBag.Competition = Competition.getCompetitionById(compId); 

      return View("EventResults"); 
     } 

您在控制器方法不起作用看到的;我認爲這是因爲該頁面並未實際「提交」?雖然我真的想使用鏈接而不是提交按鈕。有人能幫我一把嗎?

回答

1

給它像[HttpPost],[HttpGet],[HttpDelete]

[HttpPost] 
public ActionResult UpdateResults(FormCollection form, int compId, int evtId, string email) 
{ 
//Code 
} 
0

的操作類型。如果你想使用一個鏈接,您使用的是GET請求而不是POST請求。

你使用的鏈接要麼選擇使它成爲一個Ajax請求(見MVC3 Html.ActionLink Post我以前的答案)

或使用JavaScript來發布形式: How can I use an anchor tag to submit a form with jquery

 
$(document).ready(function(){ 
    $("a").click(function(){ 
    $("#requestNew").submit(); 
    }); 
}); 


or using $("#yourHrefId") if you want to refer by id rather than all hrefs.