0

I'm having a problem in my MVC project. When trying to create an object to add it to the db, it always returns null.

public class ListsModel
{
    public EntitiesList EntityList { get; set; }
    public List<string> AllGroups { get; set; }
}

    public ActionResult Create()
    {
        ListsModel model = new ListsModel();
        model.EntityList = new EntitiesList();
        model.AllGroups = managerLists.GetAllListsKeys(); //For droplist

        return View(model);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(ListsModel model)
    {
        if (ModelState.IsValid)
        {
            model.EntityList.List_CreatedTime = DateTime.Now;
            managerLists.AddNewObject(model.EntityList);
            return RedirectToAction("Index");
        }

        return View(model);
    }

And a simple cshtml:

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>EntitiesList</legend>

        <div class="form-group">
            @Html.LabelFor(model => model.EntityList.List_EntitityName)
            @Html.DropDownListFor(model => model.AllGroups, new SelectList(Model.AllGroups),
                                    new { @class = "form-control" })
            <p class="help-block">@Html.ValidationMessageFor(model => model.EntityList.List_EntitityName)</p>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.EntityList.List_EntityValue)
            <input class="form-control" value="@Model.EntityList.List_EntityValue"/>
            <p class="help-block">@Html.ValidationMessageFor(model => model.EntityList.List_EntityValue)</p>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.EntityList.List_OrderByNumber)
            <input class="form-control" value="@Model.EntityList.List_OrderByNumber"/>
            <p class="help-block">@Html.ValidationMessageFor(model => model.EntityList.List_OrderByNumber)</p>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.EntityList.List_Comments)
            <textarea class="form-control" rows="3">@Model.EntityList.List_Comments</textarea>
            <p class="help-block">@Html.ValidationMessageFor(model => model.EntityList.List_Comments)</p>
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

When it's getting to the "model.EntityList.List_CreatedTime = DateTime.Now;" a null reference exception is thrown.

I tried to change the signature to "public ActionResult Create(ListsModel ListsModel)", as suggested here: Create view is posting null objects But I got the same results.

Hope you can help me.

7
  • 2
    If you debug this what does model contain? Is model null? Is model.EntityList null? Debug is your friend! :) Place break-point on model.EntityList.List_CreatedTime = DateTime.Now; line and look at what everything is at that time. Commented May 19, 2014 at 11:21
  • Have you specified the Model in your cshtml? Commented May 19, 2014 at 11:34
  • Might be unrelated, but you should be using EditorFor, TextAreaFor etc Commented May 19, 2014 at 11:34
  • If you comment model.EntityList.List_CreatedTime = DateTime.Now; then are you still getting an error? Commented May 19, 2014 at 11:36
  • Just to clarify, ASP.NET MVC is the web front end, while Entity Framework is the database access stuff. It's a common misunderstanding; they're commonly seen together (and ASP.NET MVC projects include EF as standard) but they are actually separate. I've added the Entity Framework tag accordingly. Commented May 19, 2014 at 11:37

1 Answer 1

2

I think the problem is the way you define inputs like this:

<input class="form-control" value="@Model.EntityList.List_EntityValue"/>

For ASP MVC can collect form data, inputs should have an Name attribute corresponding with model fields.

Try to generate inputs using the standard:

@Html.TextBoxFor(model => model.EntityList.List_EntityValue)

I suggest you inspect the differences in the html generated (to see how is asp mvc generating inputs).

Sign up to request clarification or add additional context in comments.

8 Comments

IMHO this is not related to his error but worth trying.
I think it is related becouse you need a name or id on your input.
May be true cause there is no name attribute in <input> and there is also no @Model.EntityList.List_CreatedTime field in his view so there can be some problems when trying to do HttpPost...
That seems to do the job, but I'm getting an error: "System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details." I don;t know if it's related. The drop down return null as a vlue to the model, instead of the string selected, I don't understand why.
You should add a variable for the selectedvalue and then use it like this... @Html.DropDownListFor(model => model.SelectedGroup, new SelectList(Model.AllGroups, "GROUP ID COLUMN NAME", "Description COLUMN NAME", Model.SelectedGroup))
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.