0

I am new to MVC so please bear with me.

I am trying to send a string from a textbox to a controller method so I can find an object in a database. However, I do not know how to send the string successfully from the view to the controller in a HttpGet request (only in HttpPost)

The code in my view

<div>
    <label>Email</label>
    @Html.TextBox("email")
</div>
<div class="btn btn-success">
    @Html.ActionLink("Edit RSVP", "Edit")
</div>

The ViewResult method in my controller

// Problem is the email parameter is always null

[HttpGet]
public ViewResult Edit(string email)
{

    // If the email the typed is find, it will display their contents on to a RsvpForm view
    return View("RsvpForm", guestRepository.Find(email));
}

Anyone know how I can send this string through, I would be grateful.

Thanks

user3428422
  • 4,300
  • 12
  • 55
  • 119
  • 2
    Your not supposed to send the value from a form field to a controller using a get request, forms should be posted. Your get action is expecting something like `/controller/edit?email=something` – Ben Robinson Sep 17 '14 at 14:35
  • 1
    You need to wrap your code in a `form` tag and use an `input type=submit` button instead of an action link. – DavidG Sep 17 '14 at 14:36

2 Answers2

1

The easiest way to do it is to create a form as follow :

@using(Html.BeginForm("Edit", ControllerName, FormMethod.GET))
{
    @Html.Label("Email")
    @Html.TextBox("email")

    <input type="submit" value="Edit RSVP"/>
}

or you can use Jquery to change the link when textbox value change (which I do not recommend):

$('input[name=email]').on('change' function()
{
    var value = $(this).val();
    var href = $('.btn').next('a').attr('href');
    href += '?email='+value;
    $('.btn').next('a').attr('href', href)
});
Whoami
  • 334
  • 4
  • 16
1

Like this:

@using (Html.BeginForm("Edit", "ControllerName", FormMethod.Get))
{
    <div>
        <label>Email</label>
        @Html.TextBox("email")
    </div>
    <div class="btn btn-success">
         <input type="submit" value="Edit RSVP" />
    </div>
}

Note: I can't tell from your description whether or not you are trying to do this without reloading the page. This option will post the page to the controller, so you will get a page reload.

If you want this to load without posting the page, you can look into Ajax.BeginForm. Here is a StackOverflow article with a decent primer on the AJAX form.

update

For your example, you may could do something like this if you want to use AJAX. This is all untested, but may be close to what you would need.

First you can create a partial view that represents the user data that you want to display: RsvpForm.cshtml

@model GuestData 

<div class="hdr">Name</div>
<div class="value">@Model.Name</div>
<div class="hdr">Email</div>
<div class="value">@Model.Email</div>

Then you want to make sure that your controller returns the partial view based on the email that is sent via the GET: GuestDataController.cs

[HttpGet]
public ActionResult Edit(string email)
{
    // If the email the typed is find, it will display their contents on to a RsvpForm view
    return PartialView("RsvpForm", guestRepository.Find(email));
}

Then you create the AJAX form to submit the request via a GET and load the partial view without reloading the page: view.cshtml

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>

@using (Ajax.BeginForm("Edit", "GuestData", null, new AjaxOptions { UpdateTargetId = "UserData", HttpMethod = "Get" }, null))
{
    <div>
        <label>Email</label>
        @Html.TextBox("email")
    </div>
    <div class="btn btn-success">
        <input type="submit" value="Edit RSVP" />
    </div>
}

<div id="UserData"></div>
Community
  • 1
  • 1
jwatts1980
  • 7,254
  • 2
  • 28
  • 44