在MVC控制器中访问HTML表单数据

时间:2012-07-18 06:49:31

标签: asp.net-mvc asp.net-mvc-3 forms razor

我有这个问题。我需要在控制器方法期间访问用户通过语句输入的数据。

这是我的代码,也许这会让它更清晰:

// 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");
        }

您在控制器方法中看到的内容不起作用;我认为这是因为该页面实际上并未“提交”?我真的想要使用链接而不是提交按钮。有人可以帮我一把吗?

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

如果您想使用链接,则表示您正在使用GET请求而不是帖子请求。

您使用链接的选项要么将其设为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.