0

我是 aspnet mvc 的新手,我无法解决这个问题:

我在带有 2 个提交按钮的表单中有一个创建视图,其中一个是标准的保存按钮-> 验证后存储数据并重定向到索引页面。另一个保存数据,然后重定向到传递模型的同一页面,以预先填充某些表单的字段。我的问题如下:在我看来,我有一些像这样的数字输入字段:

 @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" })

这是我的控制器的一部分:

  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
        }

如果我设置属性@Value = 0,则此值会覆盖从模型传递的值,而不是如果我没有设置默认值,控制器会在尝试保存数据时给我和错误。

我该如何解决我的问题?

我想在我的控制器上保存之前将数字字段设置为 0,但这不是一种优雅的方式:D

提前致谢

4

1 回答 1

0

我们可以通过以下

查看模型和控制器操作

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>
于 2018-10-10T20:15:55.600 回答