submit form at dropdown change event in mvc4 - asp.net-mvc

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();
});

Related

Static dropdown list showing ID value instead of text on index view

How can i show the value i mean the text instead of the ID value of static dropdownlist in razor view here is the code
<td>
#Html.DisplayFor(modelItem => item.Program_duration_Years)
</td>
and here it is in create view
#Html.DropDownListFor(model=>model.Program_duration_Years, new List<SelectListItem>
{
new SelectListItem{ Text="Select Duration", Value = "0" },
new SelectListItem{ Text="One year", Value = "1" },
new SelectListItem{ Text="Two Year", Value = "2" },
new SelectListItem{ Text="Three year", Value = "3" },
new SelectListItem{ Text="Four Year", Value = "4" },
new SelectListItem{ Text="Five year", Value = "5" },
}, new { #class = "form-control", #id = "ddlduration" })
#Html.ValidationMessageFor(model => model.program_type, "", new { #class = "text-danger" })

Why is the cshtml showing links for users not in my role?

Why is the cshtml showing links for users not in my role?
To be clear non admin users should not be seeing the reset password and the create user links. In my case they are seeing the links.
In my layout.cshtml I have the following code:
#if (HttpContext.Current.User.Identity.IsAuthenticated)
{
#Html.ActionLink("My Wonderful App", "Index", "Home", new { area = "" }, new { #class = "navbar-brand" })
<span class="navbar-right">
if (HttpContext.Current.User.IsInRole("Administrators"))
{
#Html.ActionLink("Reset Password", "ResetPassword", "Auth", null, new { #class = "navbar-brand" })
#Html.ActionLink("Create User", "CreateAccount", "Auth", null, new { #class = "navbar-brand" })
}
#Html.ActionLink("Logoff", "Logoff", "Auth", null, new { #class = "navbar-brand" })
</span>
}
In my controller when the user logs in I have:
// For clarity assume:
// userName = "myTest#tester.com"
// IsAdmin = false
var identity = new ClaimsIdentity(new[] {
new Claim(ClaimTypes.Name, userName),
new Claim(ClaimTypes.Email, userName)
}, "ApplicationCookie");
if (IsAdmin)
{
identity.AddClaim(new Claim(ClaimTypes.Role, "Administrators"));
}
else
{
identity.AddClaim(new Claim(ClaimTypes.Role, "User"));
}
var ctx = Request.GetOwinContext();
var authManager = ctx.Authentication;
authManager.SignIn(identity);
The controllers are working correctly by denying access when the users click the link.
[Authorize(Roles="Administrators")]
It turned out to be a razor syntax issue.
#if (HttpContext.Current.User.Identity.IsAuthenticated)
{
#Html.ActionLink("My Wonderful App", "Index", "Home", new { area = "" }, new { #class = "navbar-brand" })
<span class="navbar-right">
if (HttpContext.Current.User.IsInRole("Administrators"))
{
#Html.ActionLink("Reset Password", "ResetPassword", "Auth", null, new { #class = "navbar-brand" })
#Html.ActionLink("Create User", "CreateAccount", "Auth", null, new { #class = "navbar-brand" })
}
#Html.ActionLink("Logoff", "Logoff", "Auth", null, new { #class = "navbar-brand" })
</span>
}
Should have been:
#if (HttpContext.Current.User.Identity.IsAuthenticated)
{
#Html.ActionLink("My Wonderful App", "Index", "Home", new { area = "" }, new { #class = "navbar-brand" })
<span class="navbar-right">
#if (HttpContext.Current.User.IsInRole("Administrators"))
{
#Html.ActionLink("Reset Password", "ResetPassword", "Auth", null, new { #class = "navbar-brand" })
#Html.ActionLink("Create User", "CreateAccount", "Auth", null, new { #class = "navbar-brand" })
}
#Html.ActionLink("Logoff", "Logoff", "Auth", null, new { #class = "navbar-brand" })
</span>
}

Passing value in the #Ajax.ActionLink Function

how to pass value in the function
#Ajax.ActionLink("Back to list", "list", "Security",
new AjaxOptions { UpdateTargetId = "show" })
There is an overload:
#Ajax.ActionLink("Back to list", "list", "Security", new { YourValue = "Value" }, new AjaxOptions { UpdateTargetId = "show" })

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
)
) %>

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