【问题标题】:Edit controller asp.net MVC (using enity framework)编辑控制器 asp.net MVC(使用实体框架)
【发布时间】:2014-02-28 13:36:37
【问题描述】:

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

我的编辑控制器如下:

 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
        }

【问题讨论】:

  • It won't accept my form on the date 这到底是什么意思?
  • 我收到消息:“字段 dateCreated 必须是日期”
  • 在我的 OP 中查看编辑
  • 看起来您的 dateCreated 属性上有 DataType.DateTime 属性
  • 文化设置似乎有问题。请使用 CUltureInfo 检查代码的文化。然后为您的设置设置适当的文化,然后日期应该接受

标签: c# asp.net asp.net-mvc datetime


【解决方案1】:

datetime 更改为nullable,它应该可以工作。这就是我的项目的工作原理。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-26
    • 2010-09-25
    • 1970-01-01
    相关资源
    最近更新 更多