Currently when I want to set html attributes like maxlength and autocomplete, I have to use the following syntax:
<%= Html.TextBox("username", ViewData["username"], new { maxlength = 20, autocomplete = "off" }) %>
Is there any way to do this without having to explicitly set the ViewData["username"] portion? In other words, I want to rely on the helper method's automatic loading routine rather than having to explicitly tell it which field to load up from the ViewData.
Just pass "null" as second parameter:
<%= Html.TextBox("username", null, new { maxlength = 20, autocomplete = "off" }) %>
yes but you have to use ViewData.Model instead of ViewData.Item()
the code in your controller should look like this (sry 4 VB.NET code)
Function Index()
ViewData("Title") = "Home Page"
ViewData("Message") = "Welcome to ASP.NET MVC!"
Dim user As New User
Return View(user)
End Function
now you can do this in the view
<%=Html.TextBox("username", Nothing, New With {.maxlength = 30})%>
note that the user object has a public property username
hth
I used construction as below:
<%= Html.TextBox("username", "", new { #maxlength = "20", #autocomplete = "off" }) %>
For Setting max length of TextBox you can pass "" or null for Second Parameter and set html attributes(maxlength) as third parameter
<%=Html.TextBox("username", "", new { #maxlength = 10 }) %>
Related
Both of these codes have the same result in the final markup. so why is the # used in the htmlAttributes section?
#Html.TextBoxFor(model => model.ManagerName, new { #autocomplete = "off", #maxlength = "40" })
#Html.TextBoxFor(model => model.ManagerName, new { autocomplete = "off", maxlength = "40" })
Does the MVC version have an effect on this?
It may have been mandatory in older versions!
In the "new { ... }" block, some words such as "class" are keywords. # is required in order to escape the keyword. For non-keywords, it doesn't make a difference.
I have this line in my view.aspx I am not using razor:
<p style="color:Red">
<% Html.DropDownListFor(model => model.AllSundays, new SelectList(Model.AllSundays)); %>
</p>
When I run my program and view source there is nothing there.
I am making use of the ViewModel in my action I have:
var allSundaysInThisMonth = new SundaysInMonthViewModel
{
AllSundays = sundays.Select(x => new SelectListItem
{
Value = x.ToString("dd-MM-yyyy"),
Text = x.ToString("dd-MM-yyyy"),
})
};
var selectedSunday = new SundaysInMonthViewModel{
SelectedSunday = thisMonthSundays.Where(x => x < now)
.Last().ToString("dd-MM-yyyy")
};
return View(allSundaysInThisMonth);
thanks
You should use <%= and no comma at the end if you want to output something to the response, otherwise you are only executing server side code that never gets flushed to the response:
<%= Html.DropDownListFor(
model => model.AllSundays,
new SelectList(Model.AllSundays)
) %>
Also AllSundays seems to already be an IEnumerable<SelectListItem> so you don't need to feed that to a SelectList in the view:
<%= Html.DropDownListFor(
model => model.AllSundays,
Model.AllSundays
) %>
Also (and this is extremely important), you should not use the same property as both the selected value and the available values. Currently you are using AllSundays twice. That's absolutely wrong. The DropDownList helper expects as first argument a scalar property on your view model to hold the selected value and an IEnumerable<SelectListItem> property as second argument that will hold the selected values.
So what you want is:
<%= Html.DropDownListFor(
model => model.SelectedSunday,
Model.AllSundays
) %>
Benefit Set: </label><br />
<%: Html.DropDownListFor(model => model.bvODSMapping.Benefit_Set, Model.BenefitSet,new {id="BSet", style = "width:230px;" })%>
When I am using model=>model.bvODSMapping.Benefit_Set.Trim()
I am getting Value Can not be null.
can anybody help me out how to trim the string?
Thanks
I guess it would be the best to trim the values before passing the model to your view.
Nevertheless, this might help:
#Html.DropDownListFor(
model =>
model.bvODSMapping.Benefit_Set,
Model.BenefitSet.Select(
item =>
new SelectListItem
{
Selected = item.Selected,
Text = item.Text.Trim(),
Value = item.Value
}),
new { id = "BSet", style = "width:230px;" })
I have an MVC 3 view with the following code:-
#using (Html.BeginForm(MVC.Order.SearchResults(), FormMethod.Get))
{
#Html.AntiForgeryToken()
#Html.Button("btnSearch", "Search", HtmlButtonType.Submit, null, new { #class = "button primary icon search", alt = "Search the orders (up to 50 characters)" }
}
When I post the form I see the __RequestVerificationToken= and the contents of the verifcation token within the querystring.
Any ideas why this may be the case and how to sort it?
Anti forgery token work only with POST requests. If you want to use them you need to change the verb used of the form to POST instead of GET:
#using (Html.BeginForm(MVC.Order.SearchResults(), FormMethod.Post))
{
#Html.AntiForgeryToken()
#Html.Button("btnSearch", "Search", HtmlButtonType.Submit, null, new { #class = "button primary icon search", alt = "Search the orders (up to 50 characters)" }
}
There is a workaround how you can pass antiforegy value through GET method or even in headers. More details here.
How can I force URL first page without page number?
Here is the exact code I use.
routes.MapRoute("MyPictureQuotes",
"picture-quotes/{PictureQuotesPage}",
new { controller = "Quote", action = "PictureQuotes", PictureQuotesPage = UrlParameter.Optional }
);
<%= Html.RouteLink("Picture Quotes", "MyPictureQuotes", null, new { title = "Picture Quotes", PictureQuotesPage = string.Empty })%>
It returns
"/picture-quotes/5" instead of
"/picture-quotes" from the page
"http://localhost:2489/picture-quotes/5"
It seems the routing value page 5 is carried over.
Does anyone have a solution for this?
You are not using the correct overload of Html.RouteLink. Use the following:
<%= Html.RouteLink("Picture Quotes", "MyPictureQuotes",
new { PictureQuotesPage = string.Empty },
new { title = "Picture Quotes" })%>