Paul Almond
Paul Almond

Reputation: 127

Get Query Parameter from URL into MVC View

I have an a href link to a page which adds a parameter to the link for example: tsw/register-your-interest?Course=979

What I am trying to do is to extract the value in Course i.e 979 and display it in the view. When attempting with the below code, I only return with 0 rather than the course value expected. ideally I'd like to avoid using routes.

Here is the view:

<div class="contact" data-component="components/checkout">

            @using (Html.BeginUmbracoForm<CourseEnquiryPageSurfaceController>("PostCourseEnquiryForm", FormMethod.Post, new { id = "checkout__form" }))
            {
                //@Html.ValidationSummary(false)
                @Model.Course;
            }

And my controller:

 public ActionResult CourseEnquiry(string Course)
    {
        var model = Mapper.Map<CourseEnquiryVM>(CurrentContent);

        model.Course = Request.QueryString["Course"];
        return model
     }

This is the View Model:

public class CourseEnquiryVM : PageContentVM
{

    public List<OfficeLocation> OfficeLocations { get; set; }
    public string Test { get; set; }
    public string Course { get; set; }
    public List<Source> SourceTypes { get; set; }
}

SOLUTION: After some research and comments I've adjusted the code to the below which now retrieves the value as expected

@Html.HiddenFor(m => m.Course, new { Value = @HttpContext.Current.Request.QueryString["Course"]});

Thanks all

Upvotes: 0

Views: 1509

Answers (2)

Kyle
Kyle

Reputation: 2018

Based on the form code you provided you need to use @Html.HiddenFor(m => m.Course) instead of just @Model.Course. @Model.Course just displays the value as text instead of building a input element that will be sent back to your controller.

If your problem is with a link prior to the view you referenced above, here's what I'd expect to work:

View with link:

@model CourseEnquiryVM

@Html.ActionLink("MyLink","CourseEnquiry","CourseController", new {course = @Model.Course}, null)

CourseController:

public ActionResult CourseEnquiry(string course)
{
    // course should have a value at this point
}

Upvotes: 1

Grizzly
Grizzly

Reputation: 5943

In your view, you are only displaying the value of Course.. which isn't able to be submitted. You need to incorporate the value of course with a form input element (textbox, checkbox, textarea, hidden, etc.).

I would highly suggest using EditorFor or Textboxfor, but because your controller action is expecting just a string parameter you could just use Editor or TextBox.

@using (Html.BeginUmbracoForm<CourseEnquiryPageSurfaceController>("PostCourseEnquiryForm", FormMethod.Post, new { id = "checkout__form" }))
{
    //@Html.ValidationSummary(false)
    @Html.TextBox(Model.Course, null, new { @class = "form-control"});

    <input type="submit" value="Submit" />
}

Then you should just be able to do this in your controller:

 public ActionResult CourseEnquiry(string course) // parameter variables are camel-case
{
    var model = Mapper.Map<CourseEnquiryVM>(CurrentContent);

    if(!string.IsNullOrWhiteSpace(course))
        model.Course = course;

    return model;
 }

Let me know if this helps.

Upvotes: 0

Related Questions