I have a problem when I want to bind a value from my model to a textbox, here is my MVC view code:
#Html.TextBox("SellerBroker", model => model.OutOfMarket.BuyerBroker.Name , new { #class = "control-label" })
I want my textbox to have a name or 'SellerBroker' and it's value to come from my model property model => model.OutOfMarket.BuyerBroker.Name and with HTML attributes of class = "control-label". However, I am receiving the following error:
Cannot convert lambda expression to type 'object' because it is not a delegate type
The #Html.TextBox() can be used for generating a textbox with an initial value (one way binding).
If you want to really bind the textbox to your class property (two ways binding), you should use the #Html.TextBoxFor() helper. This method take as parameter a lambda expression, as used in your example.
You can found more details on TextBox helpers at : Html.Textbox VS Html.TextboxFor
Helper #Html.TextBox() does not contain overloard that have lambda parameter. You should use it without lambda like this like #Stephen Muecke advice you:
#Html.TextBox("SellerBroker", Model.OutOfMarket.BuyerBroker.Name , new { #class = "control-label" })
If you want to use lambda you should use #Html.TextBoxFor() helper. But you should change name like this:
#Html.TextBoxFor(model => model.OutOfMarket.BuyerBroker.Name, new { Name = "SellerBroker", #class = "control-label"})
Related
I have my get and post methods . I populate my data during post methods based on some value. When i try to run the program it gives me a here is no ViewData item of type 'IEnumerable' that has the key because there is no data in the dropdown. How can i show empty dropdown and bind the same during post method .
The DropDownListFor template must be provided with an IEnumerable to work properly. If you want an empty list, the best way to provide an empty IEnumerable is to use the Enumerable.Empty<> static method.
Your code would then look like this:
#Html.DropDownListFor(model => model.Name, Enumerable.Empty<SelectListItem>(), "-- Select Name --", new { #class = "form-control" })
Hi I'm using Editorfor() to make a little form that submits view model to the controller. Editorfor() nicely prints input fields of the model but it also prints primary key field. So I want to hide primary key field.
#Html.EditorFor(m=>m.viewmodel)
this is markup that I have.
#Html.HiddenFor(m => m.viewmodel.Id);
#Html.EditorFor(m=>m.viewmodel)
have tried this but does not work. and I wanted to make an approach directly to the model but I'm using EF Designer, so I'm not sure where to begin. Please give me an advice.
Try this:
[HiddenInput(DisplayValue = false)]
public int ProductID { get; set;
Use a custom editor template. For example:
MyViewModel.cshtml (stored in ~/Views/Shared/EditorTemplates folder, structured like a partial view):
#model MyViewModel
#Html.HiddenFor(m => m.Id)
#Html.EditorFor(m => m.Property1)
#Html.TextboxFor(m => m.Property2)
#Html.TextAreaFor(m => m.Property3)
// Whatever else you want in the template
Then you can just call EditorFor on your model in your view that needs to use it and MVC will know to use your custom template:
#Html.EditorFor(m => m.MyViewModel)
To use a custom display template that isn't based on the name of the type, you can also use the [UIHint] attribute as described here: http://www.growingwiththeweb.com/2012/12/aspnet-mvc-display-and-editor-templates.html
Don't create any field for your key attribute. Without a field to check, the validation has nothing to complain about. Later you can supply a value for the primary key in the controller.
I have 2 fields in a MVC view.
One is #Html.TextBox("txtFirstName")
Second is hidden - #Html.TextBoxFor(model => model.FirstName, new { #type = "hidden" })
The hiddenField is there for posting purposes (as the firstTextbox can be disabled as times).
My question is How do I retrieve the client ID of first Textbox in Javascript ?? I can easily access the client ID of second textbox by '#Html.FieldIdFor(m => m.FirstName)'
The client ID has the modelName prefixed and I dont want to do any hardcoding in Javascript code.
In this case it will be txtFirstName.
But you could explicitly set it:
#Html.TextBox("txtFirstName", "some sample value", new { id = "txtFirstName" })
and then access it as usual:
var value = document.getElementById('txtFirstName').value;
or if you are using jQuery:
var value = $('#txtFirstName').val();
Use HTML helper inside your Javascript:
Javascript & Razor:
var textboxId = '#Html.IdFor(m => m.FirstName)';
There are several extensions:
#Html.IdFor(m => m.FirstName)
#Html.NameFor(m => m.FirstName)
Here is the full list of HTML helper methods http://msdn.microsoft.com/en-us/library/system.web.mvc.htmlhelper_methods(v=vs.108).aspx
Good luck
I've done this a hundred times but not sure what is going on here. I have a DropDownListFor that I populate in the controller like so
var mktsegments = from p in db.ChannelMarketSegment
where (p.ChannelCode != "0")
select p;
ViewBag.Pendist = new SelectList(mktsegments, "Pendist", "Pendist");
And in the view, I am attempting to set the default value of this drop down list with the Pendist value.
EDIT: Pendist is a field that exists in each item pulled into mktsegments via the Linq query.
<div class="M-editor-label">
#Html.LabelFor(model => model.Pendist)<span class="req">*</span>
</div>
<div class="M-editor-field">
#Html.DropDownListFor(model => model.Pendist,
(SelectList)ViewBag.Pendist, new { onchange = "ChangeChannel()" })
</div>
However, all this does is set the first value in the list as the default value. If I try to add model => model.Pendist or Model.Pendist as the third paramter in the DropDownListFor like this
#Html.DropDownListFor(model => model.Pendist,
(SelectList)ViewBag.Pendist, model => model.Pendist, new { onchange = "ChangeChannel()" })
I either get the following errors
(for model => model.Pendist)
Cannot convert lambda expression to type 'string' because it is not a delegate type
(for Model.Pendist)
'Model' confilcts with the declaration 'System.Web.Mcv.WebViewPage<TModel>.Model'
You are conflicting with the MVC ModelState. When creating Drow down lists, make sure that your property that holds the selected value is not named the same thing as your list of objects. Also, do not use a lambda for the default value, but rather just use the model item directly.
#Html.DropDownListFor(model => model.Pendist, (SelectList)ViewBag.PendistList,
Model.Pendist, new { onchange = "ChangeChannel()" })
I can do this easily using a TextBoxFor but how do I do it with an EditorFor?
I figured using the DataAnnotation [DataType(DataType.EmailAddress)] but that doesn't do it.
I don't quite understand what the DataType annotation actually does because it doesn't seem to do anything at all at first glance.
You can override the HTML Attributes, to which a browser will fallback to type='text' if they do not support it:
#Html.TextBoxFor(m => m.Email, new { #type = "email" })
it seems to be supported now.
#Html.EditorFor(model => model.Email, new { htmlAttributes = new { #class = "form-control", #type = "email" } })
The EditorFor helper method is somewhat limited out of the box and doesn't yet appear to support the HTML5 type="email" attribute.
Your options right now seem to be either using TextBoxFor or creating a custom template that will allow you to set the input's type attribute. Here's another thread that reviews some of the options for creating your own templates.
The DataAnnotation [DataType(DataType.EmailAddress)] is actually quite useful. It sets the id and name of your form field to email, which you can use with jQuery validation to show the user client-side validation messages. Applying the DataAnnotation to your model class also means that the email property on your model will be automatically validated on the server side. If you enable unobtrusive validation in your app, you get client- and servers-side validation almost for free.
As an addition to jortizromo's answer, you have now at least two options:
Specifying #type in the htmlAttributes parameter for method EditorFor() as in
#Html.EditorFor(model => model.Email, new { htmlAttributes = new { #type = "email" } })
Using the EmailAddress annotation attribute from System.ComponentModel.DataAnnotations namespace in the model class definition for the corresponding Email property and a simple call to method EditorFor() (this provides HTML validation data tags which could be a good or bad idea depending on your task) as in
ViewModel
[EmailAddress]
public string Email { get; set; }
Razor View
#Html.EditorFor(model => model.Email)