Passing value in the #Ajax.ActionLink Function - asp.net-mvc

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" })

Related

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>
}

How do you add a class and style to an HTML.ActionLink which routes to a section

I have the following action link which goes to a particular section in a view.
#Html.ActionLink("Morn Info", "Solutions", "Home", null, null, "EOB",null, null)
How do I add a style and class to this Html.ActionLink to make it look like a button?
I feel like I could add...
new { #class = "button btn-flat", #style = "border-radius: 14px;" }
but I cannot get it to work like this...
#Html.ActionLink("Early Out Billing", "Solutions", "Home", new { #class = "button btn-flat", #style = "border-radius: 14px;" }, null, "EOB", null, null)
htmlAttributes is the last value in your ActionLink constructor.
#Html.ActionLink("Morn Info",
"Solutions",
"Home",
null,
null,
"EOB",
null,
new { #class = "button btn-flat", #style = "border-radius: 14px;" })

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" />)

Resources