1

i can't seem to find the answer online...

I have a form where people can add persons. However the person I receive from the post request is empty.

My personModel has a few properties , Naam, Leeftijd and Hobbie.

My Create view has a form made with @Html.LabelFor.

Model:

public class PersoonModel
{
    public string Naam { get; private set; }
    public int Leeftijd { get; private set; }

    public string Hobbie { get; private set; }

    public PersoonModel(string naam,int leeftijd, string hobbie )
    {
        Naam = naam;
        Leeftijd = leeftijd;
        Hobbie = hobbie;
    }

    public PersoonModel()
    {

    }
}

View:

@using (Html.BeginForm("Create","Dashboard",FormMethod.Post))
{ 
  <fieldset>
    <legend>Persoon</legend>

    <div class="editor-label">
        @Html.LabelFor(model => model.Naam)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.Naam)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Leeftijd)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.Leeftijd)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Hobbie)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.Hobbie)
    </div>
    <input type="submit" value="Create new Person"/>
  </fieldset>

Controller:

    [HttpGet]
    public ActionResult Create()
    {
        PersoonModel persoonModel = new PersoonModel();
        return View(persoonModel);
    }

    [HttpPost]
    public ActionResult Create(PersoonModel Persoon)
    {
        personen.Add(Persoon);
        return Redirect("/Dashboard");
    }

I can't seem to get the layout good on stackoverflow, but I hope you understand it The persoonmodel Persoon(in my Controller) is empty

15
  • 1
    can you please also show your <form> tag (or BeginForm command, whichever you use)? Commented Feb 17, 2019 at 21:45
  • 1
    @using (Html.BeginForm("Create","Dashboard",FormMethod.Post)) { This is how I start creating my form Commented Feb 17, 2019 at 21:53
  • 1
    Please add extra code to your actual question, in the right place in the code, so it's in context and can be properly formatted - thanks. (P.S. To do that, click the "edit" button just under the little blue "c#" tag) Commented Feb 17, 2019 at 21:58
  • 1
    @SafiUllah what do you mean? You can clearly see the code is generating textboxes using HTML Helpers which should produce the correct form elements. What other info are you asking OP to provide, exactly? The form looks ok to me, it's unclear what the issue is, really. Commented Feb 17, 2019 at 22:10
  • 1
    If you're unsure what it's telling you, add a screenshot of the tools. I can't see any obvious reason why it wouldn't bind your values on postback, so we need to check that the code is really doing what it looks like it should do. (P.S. Checking your network tools is a really useful debugging technique in general with web apps) Commented Feb 17, 2019 at 22:14

2 Answers 2

2

The problem is that your setters are private and MVC model binder cannot fill-in the private values:

public class PersoonModel
{
    // remove private before set
    public string Naam { get; set; }
    public int Leeftijd { get; set; }
    public string Hobbie { get; set; }

    // more code...
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks it works! But you do not want the properties to be public set right? That's what people told me
Well, if you keep them private, then MVC model binder won't have access to bind your model... in my opinion, you need to ask those people to justify their argument with a reason :-) It is perfectly fine (and very common) to use public setters for your model properties.
Ah oki, it's the teachers who told me that, but that was when we were working in Windows forms application :)
0

I don't understand why you are using all that code in your model. I also took the liberty to change the variables to english.

Just use this

The Model

public class PersonModel
{
public string Name { get; set; }
public int PID { get; set; }
public string Hobbie { get; set; }
}

The View

// path to your personModel
@ApplicationName.ModelFolder.PersonModel 
@using (Html.BeginForm("Create","Dashboard",FormMethod.Post))
{ 
<fieldset>
<legend>Person</legend>

<div class="editor-label">
    @Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
    @Html.TextBoxFor(model => model.Name)
</div>

<div class="editor-label">
    @Html.LabelFor(model => model.PID)
</div>
<div class="editor-field">
    @Html.TextBoxFor(model => model.PID)
</div>

<div class="editor-label">
    @Html.LabelFor(model => model.Hobbie)
</div>
<div class="editor-field">
    @Html.TextBoxFor(model => model.Hobbie)
</div>
<input type="submit" value="Create new Person"/>
</fieldset>
}

and the controller

[HttpGet]
public ActionResult Create()
{
    PersonModel personModel = new PersonModel();
    return View(personModel);
}

[HttpPost]
public ActionResult Create(PersonModel model)
{
    personen.Add(model);
    return Redirect("/Dashboard");
}

This should get the values on the post function if you debug it.

3 Comments

You mean the constructor code in the Model? That's so I could set the properties private set instead of public. So I can set the properties once in the constructor
You don't need to do this explicitly in MVC. Framework does this for you. In your post function values will get set in the model automatically.
Oke , I did not know that :)

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.