ASP.NET-MVC – ASP.NET MVC model binding problem

Use this controller method: –

[AcceptVerbs(HttpVerbs.Post)]
public ViewResult Contact (Contact contactMessage)
{
return View();
}

Why do this……

public class Contact
{
public string Name {get; set; }
public string Email {get; set; }
public string Message {get; set; }
}

<% using(Html.BeginForm()) {%>

Name: <%= Html.TextBox("Name")%>


Email: <%= Html.TextBox("Email")%>


Message: <%= Html.TextBox("Message")%>


But isn’t it?

public class Contact
{
public string ContactName {get; set; }
public string ContactEmail {get; set; }
public string ContactMessage {get; set; }
}

Name: <%= Html.TextBox("ContactName")%>


Email: <%= Html.TextBox("ContactEmail")%>


Message: <%= Html.TextBox("ContactMessage")%>


Please don’t tell me that the field name is only a partial identification?

Trust me – all I did was add the “contact” text to every field in the object and every field in the form. It’s almost like MVC confuses all with the same top 7 Fields beginning with characters.

What is assigned?

Can anyone know something about this?

Paul.

I cannot currently find any reasonable explanation why The second one does not work. However, if you change the operation signature to look like this:

public ViewResult Index([Bind(Prefix="")] Contact contactMessage)

I think this is related to DefaultModelBinder.

Update:
Ok, this is really interesting at first:

// Not working
public ViewResult Index(Contact contactMessage)

// Not working
public ViewResult Index(Contact contactmessage)

/ / Working
public ViewResult Index(Contact contact)

// Working
public ViewResult Index(Contact contactMsg)

// Working
public ViewResult Index(Contact abc)

It’s really strange!

UPDATE2:

Okay, I get it. Your model has an attribute called ContactMessage, and the variable name passed in this operation is also called contactMessage. This is fuzzy The source of sex. There is no error in DefaultModelBinder. He cannot bind attributes of type string and Contact at the same time, close the case:-)

Use this controller method:-< p>

[AcceptVerbs(HttpVerbs.Post)]
public ViewResult Contact(Contact contactMessage)
{
return View();< br /> }

Why do this……

public class Contact
{
public string Name {get; set; }
public string Email {get; set; }
public string Message {get; set; }
}

<% using(Html.BeginForm()) {%>

Name: <%= Html.TextBox("Name")%>


Email: <%= Html.TextBox("Email")% >


Message: <%= Html.TextBox("Message")%>


But isn’t it?

public class Contact
{
public string ContactName {get; set; }
public string ContactEmail {get; set; }
public string ContactMessage {get; set; }
}

Name: <%= Html.TextBox("ContactName")%>


Email: <%= Html.TextBox("ContactEmail")%>


Message: <%= Html.TextBox("ContactMessage")%>


Please don’t tell me that the field name is only a partial identification?

Trust me – all I did was add the “contact” text to every field in the object and every field in the form. It’s almost like MVC confuses all with the same top 7 Fields beginning with characters.

What is assigned?

Can anyone know something about this?

Paul.

I cannot currently find any reasonable explanation why the second one does not work. However, if you will The operation signature is changed as follows:

public ViewResult Index([Bind(Prefix="")]Contact contactMessage)

I want This is related to DefaultModelBinder.

Update:
Ok, this is really interesting at first:

// Not working
public ViewResult Index(Contact contactMessage)

// Not working
public ViewResult Index(Contact contactmessage)

// Working
public ViewResult Index(Contact contact )

// Working
public ViewResult Index(Contact contactMsg)

// Working
public ViewResult Index(Contact abc)

< p>It’s really weird!

UPDATE2:

Okay, I get it. Your model has an attribute called ContactMessage, and the variable name passed in this operation is also called contactMessage. This is fuzzy The source of sex. There is no error in DefaultModelBinder. He cannot bind attributes of type string and Contact at the same time, close the case:-)

Leave a Comment

Your email address will not be published.