模型值未从VIEW发布到控制器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了模型值未从VIEW发布到控制器相关的知识,希望对你有一定的参考价值。

我正在.net core 2上工作。我过去使用过.net MVC,但没有使用.net core mvc。我在将视图中模型中的值发布到控制器时遇到麻烦。我附上代码,请检查并提供建议。

型号

  public class PagesModel
    {
        [Required(ErrorMessage = "Page Name Required")]
        [Display(Name = "Page Name")]
        public string PageName { get; set; }


        [Required(ErrorMessage = "Page Description Name Required")]
        [Display(Name = "Page Description")]
        public string PageDescription { get; set; }


        [Required(ErrorMessage = "Meta Title Required")]
        [Display(Name = "Meta Title Required")]
        public string MetaTitle { get; set; }


        [Required(ErrorMessage = "Meta Keywords Required")]
        public string MetaKeywords { get; set; }


        [Required(ErrorMessage = "Meta Description Required")]
        [Display(Name = "Meta Description")]
        public string MetaDescription { get; set; }


        [Required(ErrorMessage = "Page Content Required")]
        [Display(Name = "Page Content")]
        public string Content { get; set; }

        public bool SliderRequired { get; set; }

        public Pages page { get; set; }

        public String Error { get; set; }

        public  bool AddPage()
        {
            try { 
                page.Content = this.Content;
                page.CreatedDate = DateTime.Now;
                page.UpdatedDate = DateTime.Now;
                page.PageDescription = this.PageDescription;
                page.MetaDescription = this.MetaDescription;
                page.MetaTitle = this.MetaTitle;
                page.PageName = this.PageName;
                page.MetaKeywords = this.MetaKeywords;

                KPTWEBSITEContext db = new KPTWEBSITEContext();

                db.Pages.Add(page);
                db.SaveChanges();

                return true;

            }catch(Exception ex)
            {
                this.Error = "Could Not Instantiate Database";
                return false;
            }


        }
    }

Controller:

[Area("webmaster")]
    public class PagesController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
        public IActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public IActionResult Create(PagesModel model)
            {
                if(ModelState.IsValid)
                {
                    if(model.AddPage())
                    {
                        return Redirect("/webmaster/pages/manage");

                    }else
                    {
                        ModelState.AddModelError("_Db", model.Error);
                    }
                }

            return View("Create", model);
        }

        public IActionResult Edit(long? page_id)
        {
            return View();
        }

        public IActionResult Delete(long? page_id)
        {
            return View();
        }
    }

查看:

@model Website.Areas.webmaster.Models.PagesModel
@{

    Layout = "~/Areas/webmaster/Views/Shared/_Layout.cshtml";
}

<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <div class="content-header sty-two">
        <h1 class="text-white">Form Layouts</h1>
        <ol class="breadcrumb">
            <li><a href="#">Home</a></li>
            <li><i class="fa fa-angle-right"></i> <a href="#">Form</a></li>
            <li><i class="fa fa-angle-right"></i> Form Layouts</li>
        </ol>
    </div>
    <div class="content">
        <div class="row m-t-3">
            <div class="col-lg-12">
                <div class="card">
                    <div class="card-body">
                        <h4>Create Page</h4>
                        <p>Enter Required Details</p>
                        <form asp-action="Create" asp-controller="pages" method="post" id="createForm">
                            <input name="__RequestVerificationToken" type="hidden" value="">
                            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                            <div class="form-group">
                                <label asp-for="PageName" class="control-label"></label>
                                <div class="input-group">
                                    <input asp-for="PageName" class="form-control" />                                   
                                    <span asp-validation-for="PageName" class="text-danger"></span>
                                </div>

                            </div>
                            <div class="form-group">
                                <label asp-for="PageDescription" class="control-label"></label>
                                <div class="input-group">
                                    <input asp-for="PageDescription" class="form-control" required placeholder="Page Description" />
                                    <span asp-validation-for="PageDescription" class="text-danger"></span>
                                </div>
                            </div>
                            <div class="form-group">
                                <label asp-for="MetaTitle" class="control-label"></label>
                                <div class="input-group">
                                    <input asp-for="MetaTitle" class="form-control" required placeholder="Meta Tile" />
                                    <span asp-validation-for="MetaTitle" class="text-danger"></span>
                                </div>
                            </div>
                            <div class="form-group">
                                <label asp-for="MetaKeywords" class="control-label"></label>
                                <div class="input-group">
                                    <input asp-for="MetaKeywords" class="form-control" required placeholder="Meta Keywords" />
                                    <span asp-validation-for="MetaKeywords" class="text-danger"></span>
                                </div>
                            </div>
                            <div class="form-group">
                                <label asp-for="MetaDescription" class="control-label"></label>
                                <div class="input-group">
                                    <input asp-for="MetaDescription" class="form-control" required placeholder="Meta Description" />
                                    <span asp-validation-for="MetaDescription" class="text-danger"></span>
                                </div>
                            </div>
                            <div class="form-group">
                                <label asp-for="Content" class="control-label"></label>
                                <div class="input-group">
                                    <textarea asp-for="Content" class="form-control" required placeholder="Content" id="content_editor"></textarea>
                                    <span asp-validation-for="Content" class="text-danger"></span>
                                </div>
                            </div>
                            <div class="form-group form-check">
                                <label class="form-check-label">
                                    <input class="form-check-input" type="checkbox" asp-for="SliderRequired" /> Slider Required
                                </label>
                            </div>


                            <button type="submit" class="btn btn-success waves-effect waves-light m-r-10">Submit</button>
                            <button type="submit" class="btn btn-inverse waves-effect waves-light">Cancel</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>
@section scripts{
    <script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
    <script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>
    <script src="~/lib/Microsoft.jQuery.Unobtrusive.Ajax/jquery.unobtrusive-ajax.js"></script>
    <script src="~/assets/back/dist/plugins/ckeditor/ckeditor.js"></script>
    <script>
        // Replace the <textarea id="editor1"> with a CKEditor
        // instance, using default configuration.
        CKEDITOR.replace('content_editor');
    </script>
}

我不知道怎么了,会有所帮助的。

Controller Showing Model not posted

答案

我尝试了不带'Area'属性的代码,效果很好。也许'asp-area'标签可以解决您的问题。

以上是关于模型值未从VIEW发布到控制器的主要内容,如果未能解决你的问题,请参考以下文章

如何将 View 类中的代码片段移动到 OnAppearing() 方法?

ViewPager 未从代码、android、eclipse 更新

Core Graphics 栅格数据未从内存中释放

未从片段调用 onActivityResult

MVC 5 多选列表值未绑定到发布时的模型

来自 JQuery 填充字段的值未绑定到 MVC 模型