0

I have tried a couple of ways to deal with try fixing the problem like

Default Binder problem: https://stackoverflow.com/a/37601937/4444304

And non of them have worked... This is obviously not nested too. Which most of them are caused by nested problems...

Form.Request is getting the submitted parameters. Just the model does not seems to assign :s

Which is more weird, the field is marked as required. But the Model state is returned as Valid too.

I can't really see I can find or do further more... Thanks.

My Model looks like this
EmailSubmitModel.cs

using System.ComponentModel.DataAnnotations;

namespace myapp.Models.Home
{
    public class EmailSubmitModel
    {
        [Required(ErrorMessage = "Please enter your name")]
        public string FriendName;
    }
}

HomeController.cs

using myapp.Models.Home;

[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Submit(EmailSubmitModel emailSubmitModel)
{
    if (ModelState.IsValid)
    {
        return Json(emailSubmitModel);
    }
    return View(emailSubmitModel);
}

index.cshtml

@using myapp.Models.Home
@model EmailSubmitModel
<form asp-controller="Home" asp-action="Submit" method="post">
    <div asp-validation-summary="All"></div>
    <label asp-for="FriendName">@Localizer["Friend's Name"]</label>
    <input asp-for="FriendName" type="text" />
    <span asp-validation-for="FriendName"></span>
    <input type="submit" class="btn btn-warning">@Localizer["Send the email"]</button>
</form>
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
Eric Lam
  • 329
  • 1
  • 5
  • 13
  • Good try... but `asp-for` actually generates `name` column. I have also tried manual assigning it after your suggestion too. But no luck :( – Eric Lam Sep 30 '17 at 18:09
  • 1
    Try this: add `{ get; set; }` to the model for the `FriendName` property. – R. Richards Sep 30 '17 at 18:14
  • 1
    The model binder does not bind fields,only properties - change it to `public string FriendName { get; set; }` –  Sep 30 '17 at 20:55
  • Thanks everyone for the comments. I think I have the wrong concept about properties and fields. Should study more on C# lol – Eric Lam Oct 01 '17 at 01:52

1 Answers1

1

The FriendName should be declared as property. It is a field currently:

public string FriendName { get; set; }
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • I have accepted the answer. { get; set; } is the way to go =[]='' But `asp-for` does generates `name` field too FYR – Eric Lam Oct 01 '17 at 01:48