相信有朋友在做.NET MVC开发时,出现POST提交过数据之后,前台页面Html.TextBoxFor绑定的数据没更新,这是什么问题呢?笔者认为,这应该是你违反了.NET的约束规则,数据传递过程之中出现了丢失。

怎么解决.NET MVC 提交过数据之后页面中Html.TextBoxFor数据未更新呢?请用ModelState.Clear()试试吧。

具体解决办法是,在后台的控制器(Controller)方法中修改过数据模型之后,刷新模型的绑定状态,即使用ModelState.Clear(),让前台的数据模型重新绑定!

[HttpPost]
[ValidateInput(false)]
[AuthenticationsFilter]
public ActionResult Article(MODEL.Request.ArticleInfoQuery articleInfo)
{
    var oldArticleInfo = BLL.ContainerService._ArticleInfoService.GetModels(p => p.Id == articleInfo.Id && (p.CreateUser == Token.BaseUserId)).FirstOrDefault();
    if (oldArticleInfo != null)
    {
        oldArticleInfo.Name = articleInfo.Name;
        oldArticleInfo.ArticleCategoryId = articleInfo.ArticleCategoryId;
        oldArticleInfo.Content = articleInfo.Content;
        oldArticleInfo.Keywords = articleInfo.Keywords;
        oldArticleInfo.Description = articleInfo.Description;
        oldArticleInfo.Author = articleInfo.Author;
        oldArticleInfo.ArticleInfoType = articleInfo.ArticleInfoType;
        oldArticleInfo.CoverPicture = articleInfo.CoverPicture;
        oldArticleInfo.ReserveNum = articleInfo.ReserveNum;
        oldArticleInfo.DownPrice = articleInfo.DownPrice;
        oldArticleInfo.Size = articleInfo.Size;
        oldArticleInfo.SlideArea = articleInfo.SlideArea;
        oldArticleInfo.TopArea = articleInfo.TopArea;

        oldArticleInfo.AttachmentUrl = articleInfo.AttachmentUrl;
        oldArticleInfo.AttachmentDate = articleInfo.AttachmentDate;
        oldArticleInfo.AttachmentVersion = articleInfo.AttachmentVersion;
        oldArticleInfo.AttachmentType = articleInfo.AttachmentType;
        oldArticleInfo.AttachmentLanguage = articleInfo.AttachmentLanguage;
        oldArticleInfo.AttachmentPlateForm = articleInfo.AttachmentPlateForm;
    }

    if (string.IsNullOrEmpty(articleInfo.ArticleInfoType)) articleInfo.ArticleInfoType = BLL.Datas.BaseSiteInfo.CurrentBaseSite.BaseSiteType;

    if (oldArticleInfo == null) oldArticleInfo = Mango.Soft.Helper.Converts.ConvertObject.ToMapper<ArticleInfo, MODEL.Request.ArticleInfoQuery>(articleInfo);
    if (!string.IsNullOrEmpty(oldArticleInfo.Content)) oldArticleInfo.Content = Convert.ToBase64String(Encoding.UTF8.GetBytes(HttpUtility.UrlEncode(oldArticleInfo.Content)));

    oldArticleInfo = ContainerService._ArticleInfoService.SaveOrUpdate(oldArticleInfo, Token.BaseUser);
    articleInfo = Mango.Soft.Helper.Converts.ConvertObject.ToMapper<MODEL.Request.ArticleInfoQuery, ArticleInfo>(oldArticleInfo);
    ModelState.Clear();
    return View(articleInfo);
}

通过ModelState.Clear()方法便保证了前台页面和后台控制器中的数据一致了。.NET MVC提供了很多解决此类问题的方法,大家有空可以研究其他类似的解决方法哦!

本文.NET MVC 提交过数据之后页面中Html.TextBoxFor数据未更新,是为什么呢?IT BOY 学习の家发布的原创文章
转载请注明地址:https://www.pupilshome.com/ask/206293.html