【问题标题】:Contents not saving when edited in view在视图中编辑时不保存内容
【发布时间】:2013-07-18 13:49:16
【问题描述】:

我希望这个问题有一个简单的答案,我只是没有看到。

我正在开发一个 asp.net MVC 4 应用程序。 基本上我想允许用户编辑他们之前的条目(newsArticle)的内容(听起来很容易) 此条目由一个日期、两个文本块、(标题和正文)一个 img 组成。 这些条目对应于模型的属性,其中还包括 ID、img 名称和状态。 我已经成功地在需要时输入、保存和检索所有数据。 但是,当我运行程序并尝试编辑以前的条目时,它只会保存标题和正文,并删除那里的值,包括日期、img、img 名称和状态。

这与视图中使用的文本框有关吗?为什么保留这些值而其他值不保留。

只是指出当我运行并选择一个条目时,所有数据都正确显示,但是当我单击保存时,值被删除(日期、img 名称、状态)

任何想法.....

控制器....

            public ActionResult Edit(int id)
    {
        String encodedBody;

        NewsArticle newsArticle = _newsArticle.Get(id);

        encodedBody = HttpUtility.HtmlDecode(newsArticle.Body.ToString());

        newsArticle.Body = encodedBody;

        return View(newsArticle);
    }

    // POST: /Article/Edit/5

    [HttpPost]
    public ActionResult Edit(NewsArticle newsArticle)
    {
        try
        {
            if (ModelState.IsValid)
            {
                db.Entry(newsArticle).State = EntityState.Modified;    
                db.SaveChanges();

                return RedirectToAction("Index");
            }
        }
        catch (System.Data.DataException)
        {
            //Log th error(add a variable name after DataExpection)
            ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
        }

        return View(newsArticle);
    }

查看.......

  @using (Html.BeginForm("Edit", "Admin", FormMethod.Post, new { enctype = "multipart/form-data", @class = "form-horizontal", id = "newsEditForm"}))

{

@Html.ValidationSummary()

@Html.HiddenFor(model => model.ID)


    <div class="control-group">
        <label class="control-label">Posted on :</label>
        <div class="controls">
            <span class="text">@Model.DateCreated.Value.ToShortDateString()</span>
            @*@Html.LabelFor(n => n.DateCreated)*@
        </div>
    </div> 

    <div class="control-group">
        <label class="control-label">@Html.LabelFor(n => n.Title)</label>
        <div class="controls">

           @Html.TextBoxFor(n => n.Title, new { @class = "span4 m-wrap", rows = 1})


        </div>
    </div> 

    <div class="control-group">
        <label class="control-label">@Html.LabelFor(n => n.Body)</label>
        <div class="controls">
            @Html.TextAreaFor(n => n.Body, new { @class = "span12 ckeditor m-wrap", rows = 4 })

        </div>
    </div> 

    <div class="control-group">
        <label class="control-label">Selected Image </label>
        <label class="controls">@Html.DisplayTextFor(model => model.ImageName)</label>
   @*     <div class="span4 blog-img blog-tag-data">
            <div class="editor-field">
                <input type="file" name="Article" id="ArticleImage"/>
            </div>

        </div>*@
    </div>

    <div class="form-actions">

        <button type="submit" class="btn green" id="submitnews"><i class="icon-ok"></i>Submit</button>
         @Html.ActionLink("Cancel", "ArticleList", "Admin", null, new { @class = "btn blue"})
        @*<button type="button" class="btn blue" onclick="location.href='ArticleList','Admin'">Cancel</button>*@

    </div>                   
}

【问题讨论】:

    标签: html c#-4.0 asp.net-mvc-4


    【解决方案1】:

    您的问题是您正在保存直接发回数据库的新闻文章。已发布的 newsArticle 包含图像、日期、img 名称和状态的空条目,因为这些字段在您的表单中不存在。出于这个和其他安全原因,您真的不应该将域模型传递回您的编辑。不过,这可以快速解决您的问题

     [HttpPost]
        public ActionResult Edit(NewsArticle newsArticle)
        {
            try
            {
                if (ModelState.IsValid)
                {
    
                     NewsArticle savedArticle= _newsArticle.Get(newsArticle.Id);
                     savedArticle.Body = newsArticle.Body
                     savedArticle.Title = newsArticle.Title
                    db.SaveChanges();
    
                    return RedirectToAction("Index");
                }
            }
            catch (System.Data.DataException)
            {
                //Log th error(add a variable name after DataExpection)
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
            }
    
            return View(newsArticle);
        }
    

    【讨论】:

    • 恐怕这没有奏效,现在根本没有保存编辑,只是返回相同的数据,即进度,因为我确实需要一些数据而不是更改(日期) .如果我使用 viewMODels 或者处理这样的编辑 ActionResult 的正确方法,请按照您的答案进行。
    • 我的错,你的 100% 正确,我忘记将 int id 参数添加到 ActionResult,它工作得很好,读回来确实有意义,但我还在学习,加上这里的热量杀了我,非常感谢
    猜你喜欢
    • 2012-04-28
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多