Reputation: 2228
I'm using VSCode in Mac creating a MVC.
The parameter "new { problem = @Model }" shouldn't pass the values from the view to the controller?
View code:
@model ProblemsV4.Models.ProblemModel
<h2>New Solution</h2>
@using(Html.BeginForm("SaveSolution", "Problem", new { problem = @Model }, FormMethod.Get))
{
<label>Solution: </label>
<input type="text" name="solution" /><br/><br/>
<input type="submit" value="Save"/>
}
Controller code:
public IActionResult SaveSolution(ProblemModel problemModel, string solution)
{
SolutionModel model = new SolutionModel();
model.Solution = solution;
ProblemBC bc = new ProblemBC();
bc.AddSolution(model);
List<ProblemModel> models = bc.ListAll();
ViewBag.Message = "Solução inserida com sucesso";
return View("Index", models);
}
Upvotes: 2
Views: 2240
Reputation: 8184
You should receive a HttpPost
at the controller and send the parameters of the model with a HiddenFor
, a type of
HtmlHelper
that doesn't shows any input.
The BeginForm method has many signatures, one of them contains the Object routeValues, that is an object that contains the parameters for a route. You don't need to use that, you can use other signature like this: https://msdn.microsoft.com/en-us/library/dd460344(v=vs.118).aspx
First step:
[HttpPost]
public IActionResult SaveSolution(ProblemModel problemModel, string solution)
{
SolutionModel model = new SolutionModel();
model.Solution = solution;
ProblemBC bc = new ProblemBC();
bc.AddSolution(model);
List<ProblemModel> models = bc.ListAll();
ViewBag.Message = "Solução inserida com sucesso";
return View("Index", models);
}
Second step:
@model ProblemsV4.Models.ProblemModel
<h2>New Solution</h2>
@using(Html.BeginForm("SaveSolution", "Problem", FormMethod.Post))
{
<label>Solution: </label>
<input type="text" name="solution" /><br/><br/>
@Html.HiddenFor(model => model.Problem)
@Html.HiddenFor(model => model.Description)
@Html.HiddenFor(model => model.ID)
<input type="submit" value="Save"/>
}
Upvotes: 2