asp.net MVC3的UpdateModel排除性能不工作 [英] asp.net mvc3 UpdateModel exclude properties is not working

查看:146
本文介绍了asp.net MVC3的UpdateModel排除性能不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,它有8个道具/ 8列DB。但编辑页面上,我不想显示AddedDate或用户ID字段,因为我不希望用户更改它。

I have a class, which has 8 props / 8 columns in DB. But on a Edit page, i dont want to show the AddedDate or UserID field, since i dont want user to change it.

public class Voucher
{
    public int ID { get; set; }
    public string Title { get; set; }
    public string SiteName { get; set; }
    public string DealURL { get; set; }
    public DateTime AddedDate { get; set; }
    public DateTime? ExpirationDate { get; set; }
    public string VoucherFileURL { get; set; }
    public Guid UserID { get; set; }
}

下面是我对编辑器:

// POST: /Voucher/Edit/5

[HttpPost]
public ActionResult Edit(Voucher voucher)
{
    if (ModelState.IsValid)
    {
        string[] excludeProperties = { "AddedDate", "UserID" };
        UpdateModel(ModelState, "", null, excludeProperties);


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

在编辑页面,当我点击提交,我得到了以下错误:System.Data.SqlServerCe.SqlCeException:在转换成datetime时发生溢出

On Edit page, once i click on submit, i got the following error: System.Data.SqlServerCe.SqlCeException: An overflow occurred while converting to datetime.

好像AddedDate没有得到排除在视图模型,并引发错误。

Seems like the AddedDate didn't get excluded from the view model and triggered the error.

请你让我知道如何解决它?谢谢!

Would you please let me know how to fix it? Thanks!

public ActionResult Edit([Bind(Exclude = "AddedDate")]Voucher voucher)

没有运气不是

推荐答案

您还在传递凭证其中可能包含在它的领域。我不知道你想在这里用的UpdateModel来完​​成,如果你已经传递凭证对象?
传入凭证,将其设置为修改并保存。如果你想在数据库中使用什么,那么你就必须

You are still passing in Voucher which could contain that field in it. I'm not sure what you are trying to accomplish with the UpdateModel here if you are already passing in a Voucher object? Pass in Voucher, set it to modified and save it. If you want to use whats in the database then you'll have to


  1. 从数据库加载对象

  2. 的UpdateModel和排除性能

  3. 保存实体。

您可以简单地用一个视图模型,并张贴了。

You could simply use a View Model and post that.



public class Voucher
{
    public int ID { get; set; }
    public string Title { get; set; }
    public string SiteName { get; set; }
    public string DealURL { get; set; }
    public DateTime? ExpirationDate { get; set; }
    public string VoucherFileURL { get; set; }
    public Guid UserID { get; set; }
}

,然后从DB加载你的对象:

and then load up your object from the db":



var voucher = db.Vouchers.Where(o=>o.ID==voucherViewModel.Id);
//manually copy the fields here then save it
//copy
db.SaveChanges();

这篇关于asp.net MVC3的UpdateModel排除性能不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆