I'm newbie of aspnet mvc and I can't to resolve this issue:
I have a Create view within a form with 2 submit button, one of these is the standard save button-> store data after validation and redirect to the index page. The other one save data and then redirect to the same page passing the model in such a way to prepopulate some form's fields. My problem is the following: in my view I have some numeric input fields like this:
@Html.LabelFor(model => model.SpessoreMm)
@Html.TextBoxFor(model => model.SpessoreMm, new {@type="number", @min=0, @max=Int32.MaxValue, @Value=0, @class="form-control", style="margin: auto;"})
@Html.ValidationMessageFor(model => model.SpessoreMm, "", new { @class = "text-danger col-md-12" })
and this is part my controller:
if (submit == "Crea Nuovo") // this is the second button
{
_db.Scarico.Add(scarico);
_db.SaveChanges();
ViewBag.CaricoId = new SelectList(_registrationsManager.GetActiveKart(scarico.CaricoId), "Id", "Text", scarico.CaricoId);
return View(scarico); // if I set a breakpoint I see the model with correct value
}
If I set attribute @Value = 0 this value overrides value passed from the model, instead if I don't set a default value Controller gives me and error when tries to save data.
How can I solve my problem?
I thought to set numeric field to 0 before saving on my controller but isn't an elegant way :D
thanks in advance
We can go over the following
View Model and Controller Actions
.cshtml