0

Why am i getting the following error:

Object reference not set to an instance of an object.

I know it it becouse it tells me it' can't save it as a "null" But there are a value in it, or am i doing it wrong? I have tryed diffrent ways aswell without seeing how i should do it?

Model:

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");
        }

View:

<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>
unscope
  • 27
  • 7
  • @S.Akbari I know why i'm getting the error. I just can't fix it, this ussaly works. – unscope Apr 05 '16 at 21:10
  • 2
    You do not have any form controls with `name="Month"` and `name="Date"` (a form posts back name/value pairs based on the `name` attribute). And if you do add the name attributes, remove the pointless `Request.Form` access - the model will already be bound with the values. Finally go to the MVC site and learn the basics of creating a view using the strongly typed HtmlHelpers. –  Apr 05 '16 at 22:28
  • @StephenMuecke That was my error that i placed it as a id instad of name! Thanks alot! – unscope Apr 06 '16 at 12:20
  • dude, you have blinders on when reading comments. SM said REMOVE the Request.Form references in your code.......because that isn't proper MVC coding. – granadaCoder Apr 06 '16 at 13:07

1 Answers1

0

You really need to code defensively. And not just the "happy path".

Throw some meaningful exceptions

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");
        }
granadaCoder
  • 21,474
  • 7
  • 81
  • 117
  • I don't see there are a big diffrence on your and my code, expect you have abit validation on. And as i'm saying, i am sendinng a value with, so it should not be null – unscope Apr 05 '16 at 21:21
  • @unscope, something is happening that you don't expect. If everything is working as you expect then you would not be getting an error. So granadaCoder is saying that it is best not to assume. You will struggle less if you validate assumptions. Also, since there is no complete error message and/or indication of the specifics of the problem, we must guess at what the specific problem is. – user34660 Apr 05 '16 at 21:35
  • @user34660 The problem was that i had a id instad of a name! – unscope Apr 06 '16 at 12:35