I am having trouble passing a variable from a html form to a controller within my .NET core project. The variable I am passing to the controller is then used in an interface method call.
My data model for the form:
public class JourneyFormModel
{
public string fromStationCode { get; set; }
}
my form:
@model SplitTrainTicket.Models.JourneyFormModel
@using (Html.BeginForm("JounreyDetails", "Home", FormMethod.Post))
{
<div class="form-horizontal col-md-6">
<div class="autocomplete">
@Html.EditorFor(model => model.fromStationCode, new { htmlAttributes = new { @class = "autocomplete", id = "departStationPicker", @placeholder = "Departing From" } })
</div>
}
And my controller:
[HttpPost]
public IActionResult GetResults(JourneyFormModel form)
{
return Ok(_repository.GetAllResults(form.fromStationCode));
}
It seems that the value is null when form.toStationCode is passed to my GetAllResultsMethod. I have tired this with manually with a string and it seems to work fine. Just seems to be an issue getting the string from the form field.
This is also my interface if it is any help:
public interface IJourneyDetailsRepository
{
List<JourneyModel> GetAllResults(string fromStation);
}
Thanks in advance for anybody who reads this :)
fromStationCodeis a primary key and identity from a table, and you're putting it out there to let users change it? 2) Your form posts back to home controller jounreyDetails method but your controller example shows a different one. 3) Your controller example doesn't have [HttpPost] annotation so it's a GET method. Your form and the controller don't talk to each other.