传递给操作的空且始终有效的模型

发布于 2024-11-15 22:28:29 字数 2451 浏览 6 评论 0原文

怎么了?模型始终为空。

控制器

[HttpGet]
public ViewResult Add()
{
    return View("Edit");
}

[HttpPost]
public ViewResult Add(Models.EditViewModel m)
{
    if (ModelState.IsValid)
    {
        return View("Success");
    }
    else
    {
        return View("Edit", m);
    }
}

模型:

public class EditViewModel
{
    [Required]
    [Display(Name = "JobTitle", ResourceType = typeof(DisplayNames))]
    public string Title { get; set; }

    [Required]
    [Display(Name = "JobDescription", ResourceType = typeof(DisplayNames))]
    public string Description { get; set; }
}

视图

@model MyWebSite.Models.EditViewModel
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true, "Please correct the errors and try again.")
    @Html.EditorForModel()
    <input type="submit" value="Send" />
}

渲染视图

<script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
<form action="/MyAction" method="post"><div class="editor-label"><label for="Title">Fun&#231;&#227;o</label></div>
    <div class="editor-field"><input class="text-box single-line" data-val="true" data-val-required="Informe um valor para Fun&amp;#231;&amp;#227;o" id="Title" name="Title" type="text" value="" /> <span class="field-validation-valid" data-valmsg-for="Title" data-valmsg-replace="true"></span></div>
    <div class="editor-label"><label for="Description">Descri&#231;&#227;o</label></div>
    <div class="editor-field"><input class="text-box single-line" data-val="true" data-val-required="Informe um valor para Descri&amp;#231;&amp;#227;o" id="Description" name="Description" type="text" value="" /> <span class="field-validation-valid" data-valmsg-for="Description" data-valmsg-replace="true"></span></div>
<input type="submit" value="Send" />
</form>

What is wrong? Model is always null.

Controller

[HttpGet]
public ViewResult Add()
{
    return View("Edit");
}

[HttpPost]
public ViewResult Add(Models.EditViewModel m)
{
    if (ModelState.IsValid)
    {
        return View("Success");
    }
    else
    {
        return View("Edit", m);
    }
}

Model:

public class EditViewModel
{
    [Required]
    [Display(Name = "JobTitle", ResourceType = typeof(DisplayNames))]
    public string Title { get; set; }

    [Required]
    [Display(Name = "JobDescription", ResourceType = typeof(DisplayNames))]
    public string Description { get; set; }
}

View

@model MyWebSite.Models.EditViewModel
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true, "Please correct the errors and try again.")
    @Html.EditorForModel()
    <input type="submit" value="Send" />
}

Rendered View

<script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
<form action="/MyAction" method="post"><div class="editor-label"><label for="Title">Função</label></div>
    <div class="editor-field"><input class="text-box single-line" data-val="true" data-val-required="Informe um valor para Fun&#231;&#227;o" id="Title" name="Title" type="text" value="" /> <span class="field-validation-valid" data-valmsg-for="Title" data-valmsg-replace="true"></span></div>
    <div class="editor-label"><label for="Description">Descrição</label></div>
    <div class="editor-field"><input class="text-box single-line" data-val="true" data-val-required="Informe um valor para Descri&#231;&#227;o" id="Description" name="Description" type="text" value="" /> <span class="field-validation-valid" data-valmsg-for="Description" data-valmsg-replace="true"></span></div>
<input type="submit" value="Send" />
</form>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稀香 2024-11-22 22:28:29

知道了!
问题是一条糟糕的路线,每次都会创建一个新模型。

更改

routes.MapRoute(
    "Add", // Route name
    "AnnounceJob", // URL with parameters
    new { controller = "Jobs", action = "Add", job = new Models.EditViewModel() } // Parameter defaults
);

更改为

routes.MapRoute(
    "Add", // Route name
    "AnnounceJob", // URL with parameters
    new { controller = "Jobs", action = "Add", job = UrlParameter.Optional } // Parameter defaults
);

Got it!
The problem was a bad route that was creating a new model everytime.

Changed

routes.MapRoute(
    "Add", // Route name
    "AnnounceJob", // URL with parameters
    new { controller = "Jobs", action = "Add", job = new Models.EditViewModel() } // Parameter defaults
);

To

routes.MapRoute(
    "Add", // Route name
    "AnnounceJob", // URL with parameters
    new { controller = "Jobs", action = "Add", job = UrlParameter.Optional } // Parameter defaults
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文