获取对象引用未设置为对象的实例

时间:2016-04-05 21:07:15

标签: c# asp.net-mvc

为什么我收到以下错误:

对象引用未设置为对象的实例。

我知道它因为它告诉我'它不能将它保存为“null”但它有一个值,或者我做错了吗?我已经尝试了不同的方法,但没有看到我应该怎么做?

型号:

public class BookTime
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
        public int Zip { get; set; }
        public string City { get; set; }
        public string Address { get; set; }
        public string Month { get; set; }
        public string Date { get; set; }
    }

的Controler:

public ActionResult _BookTime(BookTime Booktime)
        {
            db.BookTimes.Add(Booktime);

            Booktime.Month = Request.Form["Month"].ToString();
            Booktime.Date = Request.Form["Date"].ToString();


            db.SaveChanges();
  return Redirect("Index");
        }

查看:

<div class="col-sm-6">
            <b>Månede</b>
            <select class="form-control" id="Month" required>
                <option value="0">Vælg Dag</option>
                <option value="Januar">Januar</option>
                <option value="Febuar">Febuar</option>
                <option value="Marts">Marts</option>
                <option value="April">April</option>
                <option value="Maj">Maj</option>
            </select>
        </div>
        <div class="col-sm-6">
            <b>Dag</b>
            <select class="form-control" id="Date" required>
                <option value="0">Vælg Dato</option>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
            </select>
        </div>

1 个答案:

答案 0 :(得分:0)

你真的需要防御性地编码。而不只是“快乐的道路”。

抛出一些有意义的例外

public ActionResult _BookTime(BookTime bt)
        {


            if(null != bt)
            {
                if(null!=Request.Form["Month"])
                {
                    bt.Month = Request.Form["Month"].ToString();
                }
                else
                {
                    throw new NullReferenceException("Month was null");
                }                   
                if(null!=Request.Form["Date"])
                {
                    /* if bt.Date is a date..what are you doing to make sure the form value is a date?? */
                    bt.Date = Request.Form["Date"].ToString();
                }
                else
                {
                    throw new NullReferenceException("Date was null");
                }               
            }
            else
            {
                throw new NullReferenceException("bt was null");
            }

            if(null!=db)
            {
            db.BookTimes.Add(bt);
            db.SaveChanges();
            }
            else
            {
                throw new NullReferenceException("Db was null");
            }
            return Redirect("Index");
        }
相关问题