Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8401257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:45:32+00:00 2026-06-09T21:45:32+00:00

Im just starting to learn MVC 3 and i could do with some advice

  • 0

Im just starting to learn MVC 3 and i could do with some advice on the best way of achieving something.

I have a basic scenario where i can create a blog post with some text and a a collection of tag objects. Each blog can have multiple tags and each tag be attatched to multiple blogs.

public class BlogPost
{
    public int ID { get; set; }
    public string BlogText { get; set; }

    public virtual ICollection<Tag> Tags { get; set; }
}

public class Tag
{
    public int ID { get; set; }
    public string TagText { get; set; }

    public virtual ICollection<BlogPost> Blogs { get; set; }
}

This creates the 2 model tables with the join table as id expect.

Whats the best way to go about adding a section to the create view of the BlogPost model that will allow me to add, edit and delete a set of tags (like in the index view)?

My initial thought would be to use a partial view of the tag object passing through the Model.Tags property but it looks like the Model property is null. Is this a good way of doing it or is there a better way?


Ok, so the action to create the view is the standard one generated:

    // GET: /Blog/Create

    public ActionResult Create()
    {
        return View();
    }

The view code is as follows:

@model MVC_ManyToManyTest.Models.BlogPost

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>BlogPost</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.BlogText)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.BlogText)
            @Html.ValidationMessageFor(model => model.BlogText)
        </div>

        <div id="HolderForPartialView">
            @Html.Partial("ViewUserControl1", Model.Tags);
        </div>

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

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

the partial view created is:

@model IEnumerable<MVC_ManyToManyTest.Models.Tag>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            TagText
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.TagText)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
            @Html.ActionLink("Details", "Details", new { id=item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ID })
        </td>
    </tr>
}

</table>

My Problem

Once I load the partial view i get the null reference at this line:

@Html.Partial("ViewUserControl1", Model.Tags);

the Model object is null

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:45:34+00:00Added an answer on June 9, 2026 at 9:45 pm

    The Model is null because your action method is not sending one in:

    return View();
    

    You need to give it an empty model with an empty collection of Tags. Something like:

    var model = new BlogPost { Tags = new Collection<Tag>() };
    return View(model);
    

    Alternatively, you can also make the creation of empty collections as part of your model constructor:

    public class BlogPost
    {
        public BlogPost()
        {
            this.Tags = new Collection<Tag>();
        }
    
        public int ID { get; set; }
        public string BlogText { get; set; }
    
        public virtual ICollection<Tag> Tags { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am just starting to learn jQuery and have gotten some of the basic
I'm just starting to learn T-SQL and could use some help in understanding what's
I am just starting to learn Python, but I have already run into some
Just starting to learn some basic C and messing around with the stat() system
I'm just starting to learn about iOS development, and I figure the best way
I'm new to ASP.NET and have just starting to learn ASP.NET MVC 3. I've
I am just starting to learn ASP.NET MVC and I have a situation where
I am just starting to learn how to use RMI, and I have a
I'm just starting to learn AspectJ, and I have use-case for say, User login.
I'm just starting to learn asp.net mvc and I'm not sure what the right

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.