Html.TextBoxFor and html.Textbox, POSTing values, model in parameters

Ok guys, I need help!

Im working with an asp.net mvc3 razor (and I'm pretty new to this, but made a lot of web forms)

Ok so to the problem

My question revolves around presentation submission. I have a very complex model based on my view (strongly typed).

I want to return the model to the arguments of the HttpPost method of the controller. mostly:

public ActionResult Personal() { DataModel dataModel = new DataModel(); FormModel model = new FormModel(); model.candidateModel = dataModel.candidateModel; model.lookupModel = new LookupModel(); return View(model); } [HttpPost] public ActionResult Personal(FormModel formModel) { if (ModelState.IsValid) { //stuff } return View(formModel); } 

Now...
I'm having trouble getting the values ​​in the formModel parameter of the post method.

This works (this means that I see this value), but it is tiring, since I need to write exactly where it sits in a row, each individual field:

 @Html.TextBox("formModel.candidateModel.tblApplicant.FirstName", Model.candidateModel.tblApplicant.FirstName) 

It looks like this:

 <input name="formModel.candidateModel.tblApplicant.FirstName" id="formModel_candidateModel_tblApplicant_FirstName" type="text" value="Graeme"/> 

This does not work:

 @Html.TextBoxFor(c => c.candidateModel.tblApplicant.FirstName) 

It looks like this:

 <input name="candidateModel.tblApplicant.FirstName" id="candidateModel_tblApplicant_FirstName" type="text" value="Graeme"/> 

Now I assume the problem is id id mismatch

So please answer me this:

  • I think about it right.
  • Why doesn't the text field get the correct / id value and how can I get it to get the correct / id value, so I can get it in POST (even if it's a problem)?
  • Also, it looks like textboxfor is restrictive, as if you were using a date. How do you use the .toshortdate () method? This makes me think that the text box is not useful to me.

Quick clarification: when I say that textboxfor is not working, it gets values ​​when I get the form. Therefore, they are filled, but in the POST / view I do not see them in the form Model in the parameters.

One more note:
None of the html helpers work, this is the problem. They also do not appear in modelstate.


Thank you all for your help.

Answer:
html.TextBoxFor and html.Textbox, POSTing values, model in parameters

This was a problem in my view somewhere, I replaced all the code with a fragment in this answer, and it worked.

Thanks again

+4
source share
3 answers

I think about it right.

Yes.

Why doesn't the text field get the correct / id value and how can I get it to get the correct / id value, so I can get it in POST (even if it's a problem)?

There is something else in your code that doesn't work. It’s hard to say since you didn’t show all your code. Here is a complete working example that illustrates and proves that something else is happening with your code:

Model:

 public class FormModel { public CandidateModel candidateModel { get; set; } } public class CandidateModel { public Applicant tblApplicant { get; set; } } public class Applicant { public string FirstName { get; set; } } 

Controller:

 public class HomeController : Controller { public ActionResult Index() { return View(new FormModel { candidateModel = new CandidateModel { tblApplicant = new Applicant { FirstName = "fn" } } }); } [HttpPost] public ActionResult Index(FormModel formModel) { // the username will be correctly bound here return View(formModel); } } 

View:

 @model FormModel @using (Html.BeginForm()) { @Html.EditorFor(c => c.candidateModel.tblApplicant.FirstName) <button type="submit">OK</button> } 

Also, it seems that textboxfor is restrictive, so what if you have a date, how do you use the .toshortdate () method? This makes me think that the text box is not useful to me.

I agree that TextBoxFor is restrictive. Therefore, I would recommend that you always use EditorFor instead of TextBoxFor . This allows you to simply decorate your view model property with the [DisplayFormat] and voilΓ  attributes. You get any format you like.

For instance:

 public class MyViewModel { [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] public DateTime CreatedAt { get; set; } } 

and in view:

 @model MyViewModel @Html.EditorFor(x => x.CreatedAt) 

will format the date exactly as you expect.

+7
source

If your view is strongly typed, try the helper below, call each helper for each property instead

 @Html.EditorForModel() @Html.EditorFor(m => m.candidateModel) @Html.EditorFor(m => m.lookupModel) 

Update:

Well, have you tried using viewmodel to simplify this task? And when you return the data, you can match your real models. keep your eyes clean, you will get fewer headaches in the future. Alternatively, you can use AutoMapper to help you.

Here is an example if you think that will help you. http://weblogs.asp.net/shijuvarghese/archive/2010/02/01/view-model-pattern-and-automapper-in-asp-net-mvc-applications.aspx

0
source

the binder model uses the name to bind values ​​to the model and html helpers, for example. Html.TextBoxFor uses the body of a lambda expression to set the name, however you can specify the name you make using Html.TextBox( helper

 @Html.TextBoxFor(x=>x.candidateModel.tblApplicant.FirstName), new{@Name="formModel.candidateModel.tblApplicant.FirstName"}) 
0
source

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


All Articles