Instead of have "disabled="true"" hardcoded in the TextBoxFor element, I want it pulled from the Model. The model property (Model.InvoicedApprovedBy) would be set to "" (enabled) or "disabled="disabled" (disabled)
<%= Html.TextBoxFor(m => m.WorkingModel.Detail.ApprovedBy, new { id = "InvoiceApprovedBy", disabled="true", style = "width: 203px; " })%>
I want to do something like this:
<%= Html.TextBoxFor(m => m.WorkingModel.Detail.ApprovedBy, new { id = "InvoiceApprovedBy", Model.InvoicedApprovedBy, style = "width: 203px; " })%>
It sets a html attribute called "InvoicedApprovedBy" instead of just placing the string "disabled="disabled"" into it. How to I get it to place the string?
<%= Html.TextBoxFor(m => m.WorkingModel.Detail.ApprovedBy, new { id = "InvoiceApprovedBy", disabled = Model.InvoicedApprovedBy, style = "width: 203px; " })%>
You won't need to set the model property to "disabled=disabled", just "disabled".
Related
I am using MVC 4, jQuery and Bootstrap plugin but I want to off the suggestion words, so I put autocomplte="off" but it is not working. Please suggestion me
My razor code is
#Html.TextBoxFor(m => m.Customer_Username, new { id = "logusrname",#autocomplete="off", #class = "form-control", #role = "form", #placeholder = "Username" })
I am trying to generate a unique label and and input text box for a partial view that is being used to render a list of user input rows.
By unique I mean that each input text box should have its unique html "id" and "name" so that when is submitted each input can be identified
In the View I have
#model UserDataModel
#{
var inpName = "benefName" + #Model.Row;
var inpAge = "benefAge" + #Model.Row;
}
#Html.LabelFor(x => x.Name, new { #class="labelhalf"})
#Html.EditorFor(x => x.Name, new { id = #inpName, htmlAttributes = new { #class = "form-control animated" } })
When the view is being render this is what I am seeing
<label class="labelhalf" for="Name">Nombre (Opcional)</label>
<input class="text-box single-line" id="Name" name="Name" type="text" value="">
As you can see the "name" and "id" attributes of the text input is "Name" and "Name" and is not using the value of the #inpName variable ("benefName1" for example)
Also I am trying to assign some CSS classes to that same input using "htmlAttributes"
I had previously tried this with this approach
<label form="FormStep_01" for=#inpName class="labelhalf">Nombre (Opcional)</label>
<input form="FormStep_01" id=#inpName class="form-control animated" pattern="^[_A-z0-9]{1,}$" type="text" placeholder="" required="">
...but the content of the input fields with this approach are not being submited and that is the reason I am trying to use the #Html.EditorFor
UPDATE
I am now using the TextBoxFor which takes the "id" and the "class" fine but not the "name" which is used in the submit
#Html.LabelFor(x => x.Name, new { #class = "labelhalf" })
#Html.TextBoxFor(x => x.Name, new { #id = #inpName, name = #inpName, #class = "form-control animated" })
Please let me know how to achieve this in MVC4
Issue 1 (Using EditorFor())
You cannot add html attributes using EditorFor() in MVC-4. This feature was not introduced until MVC-5.1, and then the correct usage is
#Html.EditorFor(m => m.SomeProperty, new { htmlAttributes = new { someAttribute = "someValue" }, })
Issue 2 (Using TextBoxFor())
You cannot change the value of the name attribute using new { name = "someValue" }. The MVC team built in a safe guard to prevent this because the whole purpose of using the HtmlHelper methods to generate form controls is to bind to your model properties, and doing this would cause binding to fail. While there is a workaround, if you do discover it, don't do it! As a side note - the following line of the private static MvcHtmlString InputHelper() method in the source code
tagBuilder.MergeAttribute("name", fullName, true);
is what prevents you overriding it.
Issue 3 (Manual html)
You not giving the inputs a name attribute. A form posts back a name/value pair based on the name and value attributes of successful controls, so if there is no name attribute, nothing will be sent to the controller.
Side note: If your manually generating html, there is no real need to add an id attribute unless your referring to that element in javascript or css.
Its unclear why your trying to create a input for something that does not appear to relate to your model, but if your trying to dynamically generate items for adding items to a collection property in your model, refer the answers here and here for some options which will allow you to bind to your model.
Im beginner in the ASP.NET MVC. I am add drop down menu , but the CSS does not work correctly. How can I fix it? This is my code:
#Html.DropDownListFor(m => Model.Fields[i], new SelectList(Model.attrs[i].atributeDetails, "AtributeDetailId", "AtDetailVal", int.Parse(Model.Fields[i])), " ", new { Class = "form-control", disabled = "disabled",style = "width:575px;height:25px;font-size:small;" })
If you're saying that the styles associated with the 'form-control' class aren't working then it could be because the 'Class' word needs to be lowercase. I normally use #class="my-class". So maybe try the following:
#Html.DropDownListFor(m => Model.Fields[i], new SelectList(Model.attrs[i].atributeDetails, "AtributeDetailId", "AtDetailVal", int.Parse(Model.Fields[i])), " ", new { #class = "form-control", disabled = "disabled",style = "width:575px;height:25px;font-size:small;" })
I have three dropdownlistfor in a loop that do not show the correct value from the DB. They always default to the first entry. I have checked and double checked the DB and verified that it should be the second one in the list. The list is also created correctly. What am I missing?
#foreach (CustomerMeasurementProfile oProfile in Model.Customer.CustomerMeasurementProfiles.Where(m => m.DeletedDate == null))
{
<div class="valuesforoneprofile form-group form-group-tight col-md-2">
<div class="col-md-11" >
#Html.Hidden(string.Format("Customer.CustomerMeasurementProfiles[{0}].Id", i), oProfile.Id)
#Html.Hidden(string.Format("Customer.CustomerMeasurementProfiles[{0}].CustomerId", i), oProfile.CustomerId)
#Html.TextBox(string.Format("Customer.CustomerMeasurementProfiles[{0}].Name", i), oProfile.Name, new { #class = "form-control input-sm" })
</div>
<div class="col-md-11" style="text-align:center">
#Html.CheckBox(string.Format("DeleteProfiles[{0}]", i), Model.DeleteProfiles[i])
</div>
<div class="col-md-11" style="padding-top:4px;">
#Html.DropDownListFor(m => oProfile.BodyTypeShoulderId, new SelectList(Model.BodyTypeShoulders, "Id", "Name"), new { #class = "form-control input-sm-select" })
</div>
<div class="col-md-11" style="padding-top:4px;">
#Html.DropDownListFor(m => oProfile.BodyTypePostureId, new SelectList(Model.BodyTypePosture, "Id", "Name"), new { #class = "form-control input-sm-select" })
</div>
<div class="col-md-11" style="padding-top:4px;">
#Html.DropDownListFor(m => oProfile.BodyTypeChestId, new SelectList(Model.BodyTypeChest, "Id", "Name"), new { #class = "form-control input-sm-select" })
</div>
If you want to set the selected value that is coming in Model. You need to do it like this:
#Html.DropDownListFor(m => oProfile.BodyTypeShoulderId,
new SelectList(Model.BodyTypeShoulders,
"Id",
"Name",
oProfile.BodyTypeShoulderId),
new { #class = "form-control input-sm-select" })
The above code will set the dropdown selected value to whatever is in the current Model object BodyTypeShoulderId
The first argument of DropDownListFor tells that on form post drop down selected value will be mapped with the Model property which is set there (we are passing m => oProfile.BodyTypeShoulderId) but this not sets selected Value.
For setting selected value you have to pass SelectList fourth parameter using this overload of SelectList class which is object selectedValue
Unfortunately #Html.DropDownListFor() behaves a little differently than other helpers when rendering controls in a loop. For a single object
#Html.DropDownListFor(m => oProfile.BodyTypeShoulderId, new SelectList(Model.BodyTypeShoulders, "Id", "Name"))
would work fine (if the value of BodyTypeShoulderId matches the value of one of the options, then that option would be selected). Ehsan has shown a work around when using it in a loop, however you have a few other issues in you code, not the least is that many of your properties will not post back correctly to a collection because your using a foreach loop rather than a for loop (which is generating duplicate name and id attributes). Your also generating a new SelectList in each iteration of the loop which is not very efficient.
You can solve both these and the dropdown selection issue by using an EditorTemplate. Assuming property CustomerMeasurementProfiles is typeof CustomerMeasurementProfiles, then
CustomerMeasurementProfiles.cshtml (add this to Views/Shared/EditorTemplates or Views/YourController/EditorTemplates)
#model CustomerMeasurementProfiles
#Html.HiddenFor(m => m.ID)
#Html.HiddenFor(m => m.CustomerId)
....
#Html.DropDownListFor(m => m.BodyTypeShoulderId, (SelectList)ViewData["shoulders"])
....
In the view model, add properties for the SelectList's and filtered collection of the items to display
IEnumerable<CustomerMeasurementProfiles> ActiveCustomerMeasurementProfiles { get; set; }
public SelectList BodyTypeShoulderList { get; set; }
....
and assign those values in the controller
Then in the main view
#using (Html.BeginForm())
{
...
#Html.EditorFor(m => m.ActiveCustomerMeasurementProfiles, new { shoulders = Model.BodyTypeShoulderList })
...
Using #Html.EditorFor() with a collection will correctly name the controls (and correctly select the right options) and on post back, ActiveCustomerMeasurementProfiles will now be correctly populated with all its properties.
In MVc I am using Html.TextboxFor() control
I have input type control as below.
<input id="phoneNumber" type="text" class="phoneNumber form-control" value="#actorPhone.PhoneNumber" data-phoneType ="#actorPhone.PhoneTypeTd" data-actorId ="#Model.ActorId" maxlength="30"/>
I want to change this intput control to MVC
#Html.TextBoxFor(m=>m.phoneNumber,new {#class = "phoneNumber", maxlength = "30"})
How I can add data-phoneType and data-actorId properties to the html control, as - is not allowed in the properties of HTML Attribute.
use an underscore _
the Razor engine is smart enough to render the underscore within the property name to the corresponding dash sign, resulting in your desired HTML5-like attributes
example:
#Html.TextBoxFor(m=>m.phoneNumber,
new {#class = "phoneNumber", maxlength = "30",
data_phoneType = YOUR_VALUE_HERE,
data-actorId=YOUR_VALUE_HERE })
and this should result in your desired output.
hope this helps :)
You can set an underscore instead and Razor will render it like a minus/dash:
#Html.TextBoxFor(m => m.phoneNumber,
new { data_phoneType = "phone type",
#class = "phoneNumber",
maxlength = "30"})
Use data_* attributes like
#Html.TextBoxFor(m=>m.phoneNumber,
new {
data_phoneType=actorPhone.PhoneTypeTd,
#class = "phoneNumber",
maxlength = "30"
})