I have a form:
#using (Html.BeginForm("QuoteUpdate", "Home", FormMethod.Post))
{
#Html.DropDownList("network", Model.availableNetworks);
#Html.DropDownList("grade", Model.grades);
#Html.HiddenFor(o => o.Product.id);
<button type="submit">Get Quote</button>
}
And a controller:
[HttpPost]
public ActionResult QuoteUpdate(int? id, string network, string grade )
{
}
The id property is always null after form is submitted. I have checked source and the hidden field has the correct value in the rendered HTML.
I cannot figure out why this parameter is always null. What am I missing?
Since you're accessing a nested property on your model, the generated HTML is probably something like this:
<input type="hidden" id="Product_id" name="Product.id" />
When your form gets posted to the controller action, there's not a parameter that matches up with Product.Id.
You could work around this by changing the generated name of the input (see this answer for more about how to do that):
#Html.HiddenFor(o => o.Product.id, new { Name = "id" });
Which will generate:
<input type="hidden" id="Product_id" name="id" />
Then things should model bind correctly.
I am trying to pass extra attribute 'data_id 'in #HTML.TextBoxFor... but I am getting no result
what I am missing in following code...
#Html.TextBoxFor(model => model._MarkScheme.MarkSchemeId, new { id = "_MarkSchemeId_Input", #class = "ElementMarkingSchemeTitle k1-grid-input k-textbox_3", data_id = #item.ElementID + "EMST"})
Many Thanks
Underscores aren't valid in HTML attribute names so Razor converts it to a hyphen. This will render with a data-id attribute.
HTML 5 data-* attributes are expected to be data_* in ASP.Net MVC view engine to render it.
HTML syntax
<input type="text" data-my-id="5" value="something" />
MVC syntax
#Html.TextBoxFor(model => model.Name, new {data_my_id=#item.ID})
Your case it is data-id
I am new to MVC. Currently I have a strongly typed view, and I use two Html.RadioButtonFor for labels, the codes are somethings like:
<label class="radio">
#Html.RadioButtonFor(model => model.Incoming, true)IncomingItems</label>
<label class="radio">
#Html.RadioButtonFor(model => model.Incoming, false)OutgoingItems</label>
<input type="hidden" id="isIncomingItem" value = "#Model.Incoming" />
What I want is to set model.Incoming to be true when the first radio button is selected or set it to false, and then I want to use this model property right away in the view( assign this value to "isIncomingItem") before the entire form is submitted to server.
Do you guys have a idea about how I could achieve it. Really appreciated!
Your Incoming property is already filled with the selected value and it will be available for the action method. And if you want to set the same value in the hidden field, try as following
#Html.HiddenFor(m => m.Incoming,new { id = "isIncomingItem"})
You'd have to use jQuery, doing something like:
$("[name='Incoming']").change(function () {
$("#isIncomingItem").val($(this).val());
});
I have a EditorFor HTML helper like this:
<td>#Html.EditorFor(m => m.Name, belowLevel ? disabledHtmlOptions : null)</td>
-
object disabledHtmlOptions = new { disabled = "disabled" };
I wanna make this disabled at every time. How do I do that? I dont want to do data annotations because this property is being used in other views too. Only on this view I want to disable it.
MVC 5.1 now allows passing in HTML attributes in EditorFor (see this answer). So you could do this:
#Html.EditorFor(model => m.Name, new { htmlAttributes = new { disabled = "disabled" } })
if you're stuck on using EditorFor you could set the disabled attribute via jQuery on page load. I know it's not ideal, but it's the only way unless you create an overload for EditorFor that accepts an htmlAttributes collection
$(document).ready(function() {
$('#Name').attr('disabled', 'disabled');
});
You could just render HTML for the view. If it's meant to be "read-only" just render the text. Otherwise you could render an <input> element.
For example, instead of
<td>#Html.EditorFor( m => m.Name )</td>
do
<td>#Model.Name</td>
or
<td><input type="text">#Model.Name</input></td>
I don't believe the signature of the EditorFor method allows you to specify any HTML attributes. You can if you change it to TextBoxFor however.
<td>#Html.TextBoxFor(m => m.Name, belowLevel ? disabledHtmlOptions : null)</td>
Is it possible when using Html.TextBoxFor to override the name attribute?
I have tried with no success. I need to use TextBoxFor to get client side validation to work, however for reasons I won't go into I need the name of the textbox to be different from the generated one.
I have tried the following:
#Html.TextBoxFor(x => x.Data, new { name = Model.Key + "_Data", id = Model.Key + "_Data" })
Which works for ID but not name. Is this possible?
Update: Looking into the code for TextBoxFor. It doesn't look like there is an easy way. Hopefully someone can prove me wrong.
Rob, actually there is a much simpler way. Instead of name, use Name:
#Html.TextBoxFor(x => x.Data, new { Name = Model.Key + "_Data", id = Model.Key + "_Data" })
Are you asking this because you want to apply a prefix to the name? If so, you can do this by setting ViewData.TemplateInfo.HtmlFieldPrefix in your Controller.
I learnt a lot about this stuff from Brad Wilson's blog.
EditorFor has an overload where you can supply the name attribute as a parameter:
#Html.EditorFor(expression, null, name)
Try EditorFor. you can pass string as template name if you want to make sure textbox is rendered even if property type is not string. If property is string already, it does not need templatename explicitly to render textbox, so you can pass null. Note that it does not require id parameter explicitly, it will infer it from element name. And all the validation things are still active with EditorFor
#Html.EditorFor(x => x.Data, "string", Model.Key + "_Data")
It is called Microsoft GOTCHA...
Use the name in caps, like this
#Html.TextBoxFor(m => m.Reply.Answer, new { Name = "Whatyouwant" })
ben's answer got me what I was looking for except you need to wrap in in Html.Raw
#Html.Raw(Html.TextBoxFor(x => x.Data).ToString().Replace("Data", "NewData"))
a little bit "unpretty"=), try:
#Html.TextBoxFor(x => x.Data).ToString().Replace("Data", "NewData")
For me, it works! I hope that help!
#Html.EditorFor(model => model.Nome, new { htmlAttributes = new { #class = "form-control", #maxlength = "80", #id = "NomeFilter", #Name = "NomeFilter" } })
#Html.EditorFor(Model => Model.Something, "name", "name", new {#class = "form-control" })
Not sure which of those two string parameters in the middle do the work, but it worked only when I typed both of them.
For this example, I was disabling form fields based on permissions, but still showing them. I had a hidden field to send the value to the controller, but wanted a different field name in the EditorFor.
First param after model value represents the "name" property, second is the new name.
#Html.EditorFor(m => m.UserName, "name", "UserNameDisabled", new { htmlAttributes = new { #class = "form-control", #disabled = "disabled"} });
Results in:
<input class="form-control text-box single-line" disabled="disabled" id="UserNameDisabled" name="UserNameDisabled" type="text" value="someEnteredValue" />
Keep it simple, your already providing the ID you should simply be able to use the method "TextBox" instead of "TextBoxFor" and it will work fine client side and server side. In addition, although the accepted answer will work but will produce duplicate Name attributes on your tag if you inspect it using a browser. The below solution does not have that problem.
MvcHtmlString Html.TextBox(string name, string value, object htmlAttributes)
#Html.TextBox(Model.Key + "_Data", Model.Key, new { id = Model.Key + "_Data" }