MrDuk
MrDuk

Reputation: 18242

Accessing HTML Form data in MVC controller

I have this problem. I need to access data that the user inputs via the statement during the controller method.

Here's my code, maybe this will make it more clear:

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

What you see in the controller method doesn't work; I assume it's because the page wasn't actually "submitted"? I really want to use a link instead of a submit button though. Can someone give me a hand?

Upvotes: 1

Views: 4450

Answers (2)

Adam Tuliper
Adam Tuliper

Reputation: 30152

If you want to use a link, you are using a GET request not a post request.

Your options using a link are either make it an ajax request (see my prior answer at MVC3 Html.ActionLink Post)

or use javascript to post the form: 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.


Upvotes: 0

Nighil
Nighil

Reputation: 4129

give it the ActionType like [HttpPost],[HttpGet],[HttpDelete]

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

Upvotes: 1

Related Questions