12 Replies - 536 Views - Last Post: 02 January 2017 - 03:20 PM

#1 Nitewalkr  Icon User is offline

  • D.I.C Lover

Reputation: 149
  • View blog
  • Posts: 1,045
  • Joined: 17-November 10

HttpPost - Null Reference issue

Posted 02 January 2017 - 01:07 AM

Hello everyone, after thinking long hard about asking for help and doing level best to fix it on my own using the resources that I have, I have decided to post here to ask for help.

The first issue is, if I remove HttpPost tag from the controller that I am working with, it works fine (although it skips couple of first db fields.) When I place HttpPost tag on the controller and try to run it, it first gave a server error that it cant find the view that I am looking for.

When I detailed the form action, it started giving me a null reference.

The problem, I am unable to work with this project when I have HttpPost tags on the controller methods.

@model myBlogProject.Models.Post

@{
    ViewBag.Title = "Create";
}

@*@using (Html.BeginForm("Create", "PostController", FormMethod.Post))
{*@
<form action="@Href("~/Post/Create")" method="post" id="postForm">
    <input type="hidden" name="PostID" value="@Model.ID" />
    <input type="text" name="Title" value="@Model.TITLE" placeholder="Title" /><br />
    <input type="text" name="Author" value="@Model.POST_AUTHOR" placeholder="Author" /><br />
    <textarea name="PostBody" rows="10" cols="80">@Model.POST_BODY</textarea>
    <input type="submit" name="submit" value="Submit!" />
@*}*@
</form>


[HttpPost]// TODO: FIX THIS ISSUE OF HTTP POST
        public ActionResult Create(string title, string author, string body)
        {
            if(!IsAdmin)
            {
                return RedirectToAction("Index");
            }
            Post post = new Post();

            if (ModelState.IsValid)
            {
                post.TITLE = title;
                post.POST_AUTHOR = author;
                post.POST_BODY = body;
                post.P_DATE_TIME = DateTime.Now;
                model.Posts.Add(post);
                model.SaveChanges();
                //return RedirectToAction("Index");
                return View(post);
            }
            return RedirectToAction("Index");
        }



I would really appreciate your help.

This post has been edited by Nitewalkr: 02 January 2017 - 01:17 AM


Is This A Good Question/Topic? 0
  • +

Replies To: HttpPost - Null Reference issue

#2 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6408
  • View blog
  • Posts: 25,889
  • Joined: 12-December 12

Re: HttpPost - Null Reference issue

Posted 02 January 2017 - 01:37 AM

Html.BeginForm("Create", "PostController", FormMethod.Post) The term "Controller" is not treated as part of the name of the Controller, it should just be "Post".
Was This Post Helpful? 0
  • +
  • -

#3 Nitewalkr  Icon User is offline

  • D.I.C Lover

Reputation: 149
  • View blog
  • Posts: 1,045
  • Joined: 17-November 10

Re: HttpPost - Null Reference issue

Posted 02 January 2017 - 03:05 AM

Yes, I tried that, it is still giving that null ref error.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6408
  • View blog
  • Posts: 25,889
  • Joined: 12-December 12

Re: HttpPost - Null Reference issue

Posted 02 January 2017 - 04:37 AM

What are the exact error details? (What is null?)
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6408
  • View blog
  • Posts: 25,889
  • Joined: 12-December 12

Re: HttpPost - Null Reference issue

Posted 02 January 2017 - 04:49 AM

Note that your textarea is named PostBody, not Body.
Was This Post Helpful? 0
  • +
  • -

#6 Nitewalkr  Icon User is offline

  • D.I.C Lover

Reputation: 149
  • View blog
  • Posts: 1,045
  • Joined: 17-November 10

Re: HttpPost - Null Reference issue

Posted 02 January 2017 - 05:18 AM

Server Error in '/' Application.

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. 

Requested URL: /Post/Create



That is the current error that I am trying to deal with. If I remove of the [HttpPost] from the controller, it would skip few fields in the post table and add to the third field, if I use [HttpPost] it would give me the stated error.

If I use the form that I have posted here, it would give the "object is not set to an instance of an object" right at the @using, but that was resolved when I scaffold the Create View.


Another Question: Do I have to have an initial data in the seed to work with? Even if I am creating an application from scratch with new database created?
EDIT: The reason I asked this, is because many(including msdn and ms sites) have a walk through where they start with initial data before they move on to create a crud and work on a table related to the same db that is being used in the application.

This post has been edited by Nitewalkr: 02 January 2017 - 05:22 AM

Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6408
  • View blog
  • Posts: 25,889
  • Joined: 12-December 12

Re: HttpPost - Null Reference issue

Posted 02 January 2017 - 05:37 AM

I suspect that if you manually create the action with action="@Href("~/Post/Create")" then it is looking for a parameterless Action Create(), and you would have to look for the posted values in the Request collection.

An alternative to listing parameters is to pass a model instance:

[HttpPost]
public ActionResult Create(Post post)

(getting rid of the manual @Href())

To use this ensure that the input names match the properties of your Post class. I would do this anyway, as a discrepancies like PostData and POST_DATA are only going to be problematic or, at least, require mapping. (Why are the properties capitalised and using underscores?)

With this approach you can also specify just the values you need (Include). Model Binding

[For a Create method I would also exclude the ID value completely. It isn't needed as you won't be attempting later to combine a Create and Edit method. If the creation fails there won't be an ID value anyway.]



It is not required to seed values to the database. If you have static lists of values then you'll want to populate these, and it is typically useful to have a few sample records available during development.
Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6408
  • View blog
  • Posts: 25,889
  • Joined: 12-December 12

Re: HttpPost - Null Reference issue

Posted 02 January 2017 - 05:42 AM

Quote

EDIT: The reason I asked this, is because many(including msdn and ms sites) have a walk through where they start with initial data before they move on to create a crud and work on a table related to the same db that is being used in the application.

Personally, I'd want to test the application at least a couple of times with completely empty data (apart from static lists), but many don't bother because the way an ORM works is that it will automatically instantiate empty lists and class instances where necessary. (The code wouldn't work anyway if this instantiation didn't happen.)
Was This Post Helpful? 0
  • +
  • -

#9 Nitewalkr  Icon User is offline

  • D.I.C Lover

Reputation: 149
  • View blog
  • Posts: 1,045
  • Joined: 17-November 10

Re: HttpPost - Null Reference issue

Posted 02 January 2017 - 06:09 AM

View Postandrewsw, on 02 January 2017 - 12:37 PM, said:

.
.
.
(Why are the properties capitalised and using underscores?)
.
.
.

First of all thanks for the link. Although this one is using the initial values for database as well, I will see if I can run work with this and will post again should I have any further questions regarding this topic.

The reason they are all capitalized is because I ran queries in MS SQL and imported the database using ADO.NET directly. I use all caps there unless I am working with sys. . Force of habit I guess, but even than one should know which one is which.
Was This Post Helpful? 0
  • +
  • -

#10 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6408
  • View blog
  • Posts: 25,889
  • Joined: 12-December 12

Re: HttpPost - Null Reference issue

Posted 02 January 2017 - 06:26 AM

You would have to constantly monitor and shepherd the difference between PostData and POST_DATA. You can map column-names:

[Column("POST_DATA")] 
[Display(Name = "Post Data")] 
public string PostData { get; set; }

Was This Post Helpful? 0
  • +
  • -

#11 Nitewalkr  Icon User is offline

  • D.I.C Lover

Reputation: 149
  • View blog
  • Posts: 1,045
  • Joined: 17-November 10

Re: HttpPost - Null Reference issue

Posted 02 January 2017 - 07:55 AM

DB migrations work just fine without initial data, for one table only. I am trying to study how to work with one to many and many to many using either partials or ViewModels(according to the current trend.)

So in code first, you don't really need to map the database out, as it is automatically being created. I have not worked with edmx files as much, where you bring your database in your project using ADO.NET, but I have seen some people, create the classes based on the database, map them out and create an initial data and some just dont bother with it.

The database in either case worked just fine. I scaffold to test and ensured that the database is mapped out correctly.

I don't understand why do you need to map out the data when ADO.NET EF creates the models for you relevant to the database and maps it out. Unless I am using FluentHibernate or NHibernate. But I think it doesn't hurt to go an extra mile to create your model classes and map it out to be safe.

You will actually laugh at the problem. Problem was just the views. Like if you create a _Layout and delete it, it will still be looking for that _Layout regardless of what type of pages you are trying to load and if you are not using the layout than you have to actually create the entire HTML page as we do, traditionally (which I noob-ly have not done.)

Thanks for help though.
Was This Post Helpful? 0
  • +
  • -

#12 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6408
  • View blog
  • Posts: 25,889
  • Joined: 12-December 12

Re: HttpPost - Null Reference issue

Posted 02 January 2017 - 08:31 AM

I know little about migrations but I will guess that after the initial creation of the ORM from the database it is possible to create a migration to specifically add attributes to the model.
Was This Post Helpful? 1
  • +
  • -

#13 Nitewalkr  Icon User is offline

  • D.I.C Lover

Reputation: 149
  • View blog
  • Posts: 1,045
  • Joined: 17-November 10

Re: HttpPost - Null Reference issue

Posted 02 January 2017 - 03:20 PM

Another reason for this issue is that, it is not taking a regular form using actions, it forces me to use the htmlhelpers. Which is kind of lame considering that I have to write a custom partial views using the form.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1