The main Asp.net model is not tied to form

I catch a mail request from a third-party static page (generated by Adobe Muse) and process it using the MVC action.

<form method="post" enctype="multipart/form-data"> <input type="text" name="Name"> ... </form> 

Routing for an empty form action:

 app.UseMvc(routes => routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}")); 

But according to the action, I have a model with any property empty

Act:

 [HttpPost] public void Index(EmailModel email) { Debug.WriteLine("Sending email"); } 

Model:

 public class EmailModel { public string Name { get; set; } public string Email { get; set; } public string Company { get; set; } public string Phone { get; set; } public string Additional { get; set; } } 

Request.Form has all the values ​​from the form, but the model is empty

 [0] {[Name, Example]} [1] {[Email, Example@example.com ]} [2] {[Company, Hello]} [3] {[Phone, Hello]} [4] {[Additional, Hello]} 
+13
source share
7 answers

Be careful not to give the action parameter a name that matches the model name, or the binder will try to bind to the parameter and fail.

 public async Task<IActionResult> Index( EmailModel email ){ ... } public class EmailModel{ public string Email { get; set; } } 

Change the email parameter to a different name and bind it as expected.

 public async Task<IActionResult> Index( EmailModel uniqueName ){ ... } 
+31
source

I'm not sure if this is the same case, but I had the same problem, and it really doesn't seem to me that this works.
The problem in my case was that I had a Model property in my model model class

 public string Model { get; set; } 

When I renamed the property to ModelName, everything worked fine again, even without the FromForm attribute.

It looks like some special property names might be a problem for binding the asp.net mvc model.

So, my advice is to check your model properties and maybe try renaming them one by one to see if there is a problem.

Hope this helps.

+1
source

Change void to ActionResult.

 [HttpPost] public ActionResult Index(EmailModel email) 

And don't forget to check AntiForgeryToken on your view and action.

 // to your form in view @Html.AntiForgeryToken() // ------------ [HttpPost] [ValidateAntiForgeryToken] public ActionResult Index(EmailModel email) 
0
source

I have the same problem these documents help me understand the model binding https://docs.asp.net/en/latest/mvc/models/model-binding.html

I solved my problem by making sure that the property name is an exact match in the form field name and I also add the [FromForm] attribute to specify the exact source of the binding.

0
source

This problem can also occur if one or more of your properties in the query model cannot be tied to what is acceptable in the query model.

In my case, I had to pass the List<string> property, but accidentally passed a string . This made the whole query model null

0
source

I came across this today, and although this seems obvious in retrospect, I thought I'd add that you need to make sure that your access modifiers for the properties in the model you are binding are correct. I had public MyProperty { get; internal set; } public MyProperty { get; internal set; } public MyProperty { get; internal set; } public MyProperty { get; internal set; } public MyProperty { get; internal set; } public MyProperty { get; internal set; } public MyProperty { get; internal set; } public MyProperty { get; internal set; } public MyProperty { get; internal set; } on some, and they will not bind. I removed internal and they worked just fine.

0
source

In my case, I used the MVC 4.0 convention to name the object you are posting. For instance,

js: $http.post("SaveAnswer", { answer: answerValues })

C #: public ActionResult SaveAnswer([FromBody] AnswerVM answer) {...}

When I changed js to $http.post("SaveAnswer", answerValues) , all $http.post("SaveAnswer", answerValues) .

0
source

Source: https://habr.com/ru/post/1258591/


All Articles