0

I'm trying to create tags for a blog project. I want the user to enter the tags, and then I want to display all the tags they enter. Right now, I'm struggling to just get that to work.

I keep getting a "System.NullReferenceException" in the view on the @Html.EditorFor line:

@model BlogNiKRaMu.Models.TagAdding

@{
    ViewBag.Title = "AddTag";
    var add = Model.Add;
}

<h2>AddTag</h2>

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    
    <div class="form-horizontal">
        <h4>Post</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        
        @if (Model.Tag?.Count > 0)
        {
            <p>
                Tags: @foreach (var item in Model.Tag)
                {
                    @item.Name
                })
            </p>

        }
        
        **@Html.EditorFor(model => model.Tag[add].Name)**

        @*<input type="Text" name="tagInput" value="Input Tag" />*@

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}

This is the Controller ActionResult methods:

public ActionResult AddTag()
        {
            TagAdding tagg = new TagAdding();
            tagg.Add = 0;
            return View(tagg);
        }

        [HttpPost]
        public ActionResult AddTag(TagAdding tagg)
        {


            tagg.Add++;

           return View(tagg);

        }

This is my model:

public class TagAdding
    {

        public Post Post { get; set; }
        public List<Tag> Tag { get; set; } = new List<Tag>();


        public int Add { get; set; }

    }

How can I fix this?

Also, I don't even know if my code is the best way to achieve what I'm trying to do. I'd be more than happy to learn new things:)

CoolCode
  • 1
  • 2
  • 1
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Yong Shun Jun 06 '21 at 10:43
  • model.Tag is null I presume. Instantiate it in the model beforehand? – SnowGroomer Jun 06 '21 at 10:43
  • Thanks for your quick answers! Ill try to instantiate the list in the model. Edit: I instantiated the List in my model, but now I get the same exception, but it happens immidiately – CoolCode Jun 06 '21 at 10:46

1 Answers1

0

first, it would be really useful if you added your Model (so the BlogNiKRaMu.Models.TagAdding class).

Since you tagged your post with EF Core, I assume the issue lies in your entity class not having a default value on the navigation property to the Tags table. Then also, to load and store tags, you should either (auto-)include the tags, or at least store them accordingly in the Update/Save method (however you wanna call it) of your ITagService and its underlying repositories.

Edit 1: Thanks for your update regarding the model. I honestly do not understand what you are trying to do here. Maybe you should re-structure the code this way:

  1. The model (assuming you are using MVC) should contain the post and the current tags.

  2. You should define a submit model, which is only used for the form you are displaying in your view. This must be separate from the model you are using to display the page. It should probably contain:

    • public Post Post { get; set; } set from your view model
    • public string Name { get; set; } set from the form data, and represents the tag name you want to add
  3. Then use this submit model as a parameter in the public ActionResult AddTag([FromBody]TagAddModel formContent), and in the body, the operation becomes more complex:

    • Check if a tag with the same name exists in database
    • If not, create that tag in a separate database table
    • Then use the result of that "GetOrCreate" operation, which is of type Tag, and add it to the post, if not already present on that post
    • Then persist everything to the database through the EF context

Disclaimer: If you have absolutely no idea what I am talking about, then I suggest that you start with something less difficult than a blog. To have a maintainable project that is fun is the most important thing when developing software, and you really need design strategies to make your project work.

Anyway, best of luck, hope it helps!

phimath
  • 141
  • 8