Aspnet MVC: Problem with default value of textboxfor

308 Views Asked by At

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

1

There are 1 best solutions below

0
kblau On

We can go over the following

View Model and Controller Actions

public class scarico
{
    public int SpessoreMm { get; set; }
}

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Tut129(scarico solidoViewModel, string submit)
    {
        if (submit == "Crea Nuovo") // this is the second button 
        {
            if (ModelState.IsValid)
            {
                //add the entity here
                //_db.Scarico.Add(scarico);
                //_db.SaveChanges();
            }

            //ViewBag.CaricoId = new SelectList(_registrationsManager.GetActiveKart(scarico.CaricoId), 
            //    "Id", "Text", scarico.CaricoId);
            return View(solidoViewModel); // if I set a breakpoint I see the model with correct value
        }

        //first button pushed so *save data* and redirect to index
        return RedirectToAction("Index");
    }

    public ActionResult Tut129()
    {
        //Not initalizing property, so non null value type int will be zero
        scarico solidoViewModel = new scarico();
        return View(solidoViewModel);
    }

.cshtml

@model Testy20161006.Controllers.scarico
    @{
        Layout = null;
    }

    <!DOCTYPE html>

    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Tut129</title>
    </head>
    <body>
        @using (Html.BeginForm())
        {

            @Html.LabelFor(model => model.SpessoreMm)
            @Html.TextBoxFor(model => model.SpessoreMm,
                //!!! Removing @Value = 0, and using passed value from action, if not set in action then non
                //null value type int will be zero
        new { @type = "number", @min = 0, @max = Int32.MaxValue, @class = "form-control", style = "margin: auto;" })
            @Html.ValidationMessageFor(model => model.SpessoreMm, "", new { @class = "text-danger col-md-12" })

            <input type="submit" name="submit" value="Standard Save" />
            <input type="submit" name="submit" value="Crea Nuovo" />
        }
    </body>
</html>