Html Helper with special htmlattributes - asp.net-mvc

Does anyone know how to pass html attributes like "data-date" to the Html.TextBox()??
It is used from the bootstrap datepicker.
Is there a way around it?
#Html.TextBoxFor(p => p.DateFrom, new { #class = "small", type = "date", "data-date"="12-02-2012" })

You have to use underscore instead of '-'.
So what you're after would go something like this:
#Html.TextBoxFor(p => p.DateFrom, new { #class = "small", #type = "date", #data_date="12-02-2012" })

Use:
new Dictionary<string, object> { { "class", "small" }, {"type", "date"} { "data-date", "12-02-2012" } }
instead of:
new { #class = "small", type = "date", "data-date"="12-02-2012" }

I was trying to add the date picker to the Html helper using bootstrap. rudeovski ze bear's answer made it easier.
This is how it worked for me.
#Html.TextBox("startdate", null
, new {#class = "type", #type = "date", #data_date="12-02-2012" })

Related

need help: editor and numbers asp.net

I have an editor field in asp.net which needs to be integer input only between 1 and the size of a list. Atm i got this as code
#Html.Editor("Prioriteit" + item, new { htmlAttributes = new { #type = "number", min = "0",step = "1", value = "0" } })
this isn't to the maximum of the list, I know this. However when i open the page i can still add text.
View with text in editor
Someone can help me out?
thanks in advance
Changed the code for the sake of someone linking it to another question
code
#Html.TextBox("Prioriteit" + item,null, new { htmlAttributes = new { #type = "number", min = "0",step = "1", value = "0" } })
still the same
`
You can also do the following:
<input type="number" min="0" step="1" value="0" name="Prioriteit#item" />
Try this:
#Html.TextBox("Prioriteit" + item, "0", new { #type="number", min = "0", step = "1" })

submit form at dropdown change event in mvc4

here is my code
<div id="pro_pag2">
#using (Html.BeginForm("Product", "Main", new { customerId = mdlLoginData.CustomerID, GroupID = ViewBag.GroupID, page = 1 }, FormMethod.Post, new { }))
{
#Html.DropDownList("PageSize", new SelectList(new Dictionary<string, string> {{ "18", "18" }, { "12", "12" },{ "6", "6" } }, "Key", "Value"), new { #class = "pro_pag_tf1" })
}
</div>
my question is that how do I submit form at selection change of the dropdown
You can use jQuery. Give the drop down list an id, like so.
<div id="pro_pag2">
#using (Html.BeginForm("Product", "Main", new { customerId = mdlLoginData.CustomerID, GroupID = ViewBag.GroupID, page = 1 }, FormMethod.Post, new { }))
{
#Html.DropDownList("PageSize",
new SelectList(new Dictionary<string, string> {...}, "Key", "Value"),
new { #class = "pro_pag_tf1", id = "pagesizelist" })
}
</div>
Then use jQuery to submit its parent form
$('#pagesizelist').on('change', function(event){
var form = $(event.target).parents('form');
form.submit();
});

Identifier expected error in ascx page in MVC

I am getting BC30203 error in my ascx page.
BC30203: Identifier expected. (Line 4 - new[] )
Code:
<%= Html.DropDownList(
"",
new SelectList(
new[]
{
new { Value = "true", Text = "Yes" },
new { Value = "false", Text = "No" },
},
"Value",
"Text",
Model
)
) %>
What is missing ?
You are missing what to create:
new SelectList(
new ListItem[]
{
new ListItem { Value = "true", Text = "Yes" },
new ListItem { Value = "false", Text = "No" },
}
When using the new keyword, you must tell the compiler what you want to create it won't take a guess.
A red-flag that I see is that you are not giving a name to your SelectList.
<%= Html.DropDownList("MySelect",
new SelectList(
new[]
{
new SelectListItem() { Value = "true", Text = "Yes" },
new SelectListItem() { Value = "false", Text = "No" },
},
"Value",
"Text",
Model
)
) %>
The DropDownList method requires an IEnumerable<SelectListItem> as the 2nd parameter.
Try something like this
<%= Html.DropDownList(
"Name",
new List<SelectListItem>()
{
new SelectListItem() { Value = "true", Text = "Yes" },
new SelectListItem() { Value = "false", Text = "No" },
},
"Value",
Model
)
) %>

MVC 3 Webgrid - how do you hide columns you do not want to be visible?

I have a webgrid and there is a column I want to be visible only to certain users.
Currently I have coded the grid as follows
if (Context.User.IsInRole(Role.Inputter) || Context.User.IsInRole(Role.Administrator))
{
#grid.GetHtml(columns: grid.Columns(
grid.Column(format: (item) => Html.ActionLink("Select", "Details", new { contractId = item.ContractId })),
grid.Column(format: (item) => Html.ActionLink("Edit", "Edit", new { contractId = item.ContractId })),
grid.Column("SignOffDate", "Sign Off Date",
format: #<text> <span>#item.SignOffDate.ToString("d/M/yyyy")</span></text>),
grid.Column("FullContractNumber", "Contract Number"),
grid.Column("ContractTitle", "Title")
));
}
else
{
#grid.GetHtml(columns: grid.Columns(
grid.Column(format: (item) => Html.ActionLink("Select", "Details", new { contractId = item.ContractId })),
grid.Column("SignOffDate", "Sign Off Date",
format: #<text> <span>#item.SignOffDate.ToString("d/M/yyyy")</span></text>),
grid.Column("FullContractNumber", "Contract Number"),
grid.Column("ContractTitle", "Title")
));
}
But surely there is a better way without repeating all that code?
The only difference between the 2 column inputs is that I want to display the Edit link for particlaur users. So what is the best alternative way of doing that?
Try like this (untested, don't have access to VS at the moment):
#{
var gridColumns = new List<WebGridColumn>();
gridColumns.Add(grid.Column(format: (item) => Html.ActionLink("Select", "Details", new { contractId = item.ContractId })));
if (Context.User.IsInRole(Role.Inputter) || Context.User.IsInRole(Role.Administrator))
{
gridColumns.Add(grid.Column(format: (item) => Html.ActionLink("Edit", "Edit", new { contractId = item.ContractId })));
}
gridColumns.Add(grid.Column("SignOffDate", "Sign Off Date", format: #<text> <span>#item.SignOffDate.ToString("d/M/yyyy")</span></text>));
gridColumns.Add(grid.Column("FullContractNumber", "Contract Number"));
gridColumns.Add(grid.Column("ContractTitle", "Title"));
}
#grid.GetHtml(columns: grid.Columns(gridColumns.ToArray()));
Not sure if it can me made more simpler like this by using "columnNames" parameter. I wanted to show "CustomerCode" column so have just put "CustomerCode" any other column gets excluded.
WebGrid obj = new WebGrid(Custs,columnNames: new[] { "CustomerCode"});
Taken from
http://www.codeproject.com/Articles/843788/WebGrid-in-ASP-NET-MVC-important-tips#Tip3:-DisplayNecessaryColumnsMVCWebGrid
grid.Column("FriendlyId", style:"hidecol",header:"")
Instead of using it like this you should use it like in the manner bellow. I've tried, it will work successfully.
grid.Column(format: #<input type="hidden" name="FriendlyId" value="#item.FriendlyId" />)

Html.DropDownList binding integers

I have in my controller following code:
Values = new SelectList(new[] {10, 25, 50});
In my view I have:
<%= Html.DropDownList("selItemsPerPage1", Model.Items.Values,
new {
onchange="something", title="something"
});
In result output it displays list of
System.Web.Mvc.SelectList.
How do I make it display integers instead?
In the controller:
Values = new SelectList(
new[]
{
new SelectListItem { Value = "10", Text = "10" },
new SelectListItem { Value = "25", Text = "25" },
new SelectListItem { Value = "50", Text = "50" },
},
"Value", "Text"
);
or if you prefer:
var values = new[] { 10, 25, 50 }.Select(x => new SelectListItem
{
Value = x.ToString(),
Text = x.ToString()
});
Values = new SelectList(values, "Value", "Text");
and in the view:
<%= Html.DropDownList(
"selItemsPerPage1",
Model.Items.Values,
new {
onchange = "something",
title = "something"
}
) %>

Resources