1

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

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

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

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

4

2 回答 2

1

给它 ActionType 喜欢 [HttpPost],[HttpGet],[HttpDelete]

[HttpPost]
public ActionResult UpdateResults(FormCollection form, int compId, int evtId, string email)
{
//Code
}
于 2012-07-18T06:53:55.330 回答
0

如果要使用链接,则使用的是 GET 请求而不是发布请求。

您使用链接的选项是使其成为 ajax 请求(请参阅我之前在MVC3 Html.ActionLink Post的回答)

或使用 javascript 发布表单: How can I use an anchor tag to submit a form with jquery

$(文档).ready(函数(){
  $("a").click(函数(){
     $("#requestNew").submit();
  });
});


或使用 $("#yourHrefId") 如果您想通过 id 而不是所有 href 引用。


于 2012-07-18T19:04:41.243 回答