0

I am using Visual Studio 2013 with Framework 4.

I have one model with attribute firstname.

This is my model

[Required]
[Display(Name = "Investigator")]
public string FirstName { get; set; }

This is my view where I am using:

In dropdownlist:

@Html.DropDownListFor(model => model.FirstName, Model.Names, "Enter New ...", new { @id = "ddlnames" })
@Html.ValidationMessageFor(model => model.FirstName)

In textbox:

@Html.EditorFor(model => model.FirstName)

I am using it for dropdownlist selection and in dropdownlist I have one option to add new first name and on click on that I am giving option for add new First Name with text box having same name means (FirstName) as model of razor for new entry.

So my question is can I use that FirstName attribute of model for dropdownlist selection and for new entry as well.

I am getting value in dropdownlist selection but when I enter new first name value not coming.

Please someone who knows it help me to solve this.

Thanks in advance.

3 rules
  • 1,359
  • 3
  • 26
  • 54
  • Short answer is no (the `DefaultModelBinder` will only bind the first name/pair value that is submitted (unless the property were `string[] FirstName`), As always use a view model. –  Jul 06 '16 at 10:17
  • You can not do that.Add an extra property in you model for firstname textbox – Shashank Sood Jul 06 '16 at 10:18
  • Ohh sad to know that this is hard for me to do because I am sending the whole model by using pure mvc at server side so I can not change model still I will try by doing some tricky way. thanks Stephen Muecke and Shashank Sood for the answer. – 3 rules Jul 06 '16 at 10:23
  • Is it possible to set that textbox value in FirstName of model by using javascript or jquery? – 3 rules Jul 06 '16 at 10:24
  • @padhiyar, Stop using a data model in a view. Always use a view model. –  Jul 06 '16 at 10:27
  • I can't understand view model? Can you give me one link please so I can view and use it! – 3 rules Jul 06 '16 at 10:28
  • [What is ViewModel in MVC?](http://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  Jul 06 '16 at 10:36
  • Ohhh thank you again @StephenMuecke – 3 rules Jul 06 '16 at 10:45

0 Answers0