繁体   English   中英

编辑控制器 asp.net MVC(使用实体框架)

[英]Edit controller asp.net MVC (using enity framework)

在此处输入图片说明我的“编辑视图”有问题。 我可以创建新项目并查看它们没有问题。 我也可以进入编辑表单并查看所有内容并进行更改。 但出于某种原因,它不会在该日期接受我的表单,即使它与我用来创建条目的日期相同。

我的编辑控制器如下:

 public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            News news = db.News.Find(id);
            if (news == null)
            {
                return HttpNotFound();
            }
            return View(news);
        }

        // POST: /Newss/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include="ID,title,body,category,dateCreated")] News news)
        {
            if (ModelState.IsValid)
            {

                db.Entry(news).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(news);
        }

我的编辑视图如下:

@model NewsFeed.Models.News

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>News</h4>
        <hr />
        @Html.ValidationSummary(true)
        @Html.HiddenFor(model => model.ID)

        <div class="form-group">
            @Html.LabelFor(model => model.title, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.title)
                @Html.ValidationMessageFor(model => model.title)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.body, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.body)
                @Html.ValidationMessageFor(model => model.body)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.category, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.category)
                @Html.ValidationMessageFor(model => model.category)
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.dateCreated, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.dateCreated)
                @Html.ValidationMessageFor(model => model.dateCreated)
            </div>
        </div>


        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

编辑

模型:

public class News
    {
        public int ID { get; set; }
        [Required(ErrorMessage = "Title is Required")] 
        public string title { get; set; }
        [Required(ErrorMessage = "Body is Required")] 
        public string body { get; set; }
        public Category category { get; set; }
        public DateTime dateCreated { get; set; }


        public enum Category 
        {
            Sports,
            News,
            Politics,
            Education
        }

datetime更改为nullable ,它应该可以工作。 这对我的项目有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM