I'm currently working on a website in asp.net MVC. I have one view to edit my model but some item in this model can only be modified by admins.
So I removed some editorfor() for the basic users and everything works fine...
Until I saw that : if basic user edit the model, the fields which are hidden for them are modified to a default value not the previous one modified by an admin.
Did someone know how can I saved the previous values even they are not in an editorfor() ?
Code :
#Html.EditorFor(model => model.name)
#if (ViewBag.Role == "Admin")
{
#Html.EditorFor(model => model.age)
// If admin is connected, he can edit age => no problem
}
// if a user modify name but don't touch age because it's for admin only, the model age will get 0
Thanks in advance
#Html.HiddenFor(m = m.age)
Simple Answer for my stupid question, I have tried it on first but it didn't work but now it did so there is the solution. Thanks to Stephen Muecke.
#Html.EditorFor(model => model.name)
#if (ViewBag.Role == "Admin")
{
#Html.EditorFor(model => model.age)
// If admin is connected, he can edit age => no problem
}
If we take your example then age is only for admin. so when basic user will edit this page that time condition will not satisfied and #Html.EditorFor(model => model.age) will be not rendered for basic user. So when you post the form that time age's value will not be pass to controller because age is not rendered in form. so it will save default value which you set.
Solution is you have to use hidden field out side the if condition. so value will be keep as it is controller -> View -> controller.
Related
Can someone please help me figure this out. I have a Drop Down List in ASP.NET Razor code
#Html.DropDownListFor(model => model.Height, heightlist, new { style = "font-size:120%;" })
My Drop Down List is too big when you click on the list. I would like it to be like the one on the right hand side:
I tried various methods, including the code below, but the issue exists:
#Html.DropDownListFor(model => model.Height, heightlist, new { height="50", style = "font-size:120%; height:30px;" })
In my case. I have login form for sign up the form has some field like phone-number, e-mail address, password etc.. after sign up when the user want to login the requirement is e-mail address and password. Chrome automatically filled the fields password field is ok but in the e-mail field automatically phone number pasted.
How do i configure this ?
The shots shows chrome credential when i sign up the site.
Last one is true i want to saved like last one.
Form markup, that's the form phone and e-mail inputs code
#Html.TextBoxFor(m => m.Email, new { #class = "SSTinputTextE input", placeholder = "E-POSTA ADRESİ" })
#Html.ValidationMessageFor(m => m.Email, "", new { #class = "text-danger", #style = "display:none" })
#Html.TextBoxFor(m => m.Phone, new { #class = "SSTinputTextE input txtNumeric", placeholder = "CEP TELEFONU", maxlength = "11" })
#Html.ValidationMessageFor(m => m.Phone, "", new { #class = "text-danger ", #style = "display:none" })
I had the exact same problem in a .Net MVC site with very similar markup. I also use placeholder attributes on the input element itself, instead of label elements before the element—just like the op.
I fixed the problem by moving the phone number field before the email field in the HTML (and form visually, if that matters).
I didn't dig into it enough to figure out the exact reason for this definitively, but I suspect it was because it was picking up the first input field after anything with the word "email" in it. Since there wasn't a label for the phone number to interrupt this logic, the phone number field was next so it chose that. I know Chrome uses some simple regex for picking up these auto save fields, so maybe its just not clever enough to realize that placeholders on the element itself can count too? (Again, this is just a guess at the reason. But my workaround above worked anyway.)
I am using a Telerik Grid to display data to the client. I have to show priority values first and then display non priority values. When the user group base on priority, the priority values should group first follow by the non priority group. I have a default descending grouping. When the user first access the page, it works fine. However, if the user remove the default grouping and try to group that column again, the non priority values are shown in the first group following by the priority which is the opposite of what I want.
In addition, I tried to do it on the client side using jquery, but the grid variable is always return null.
$(function () {
var grid = $('#Shipping').data('tGrid);
alert(grid) // always return null.
});
Here is the client side code that I am using for that column.
#(Html.Telerik().Grid(Model)
.Name("Shipping")
.DataKeys(Keys =>
{
Keys.Add(c => c.ShippingID);
})
.DataBinding(databinding => databinding.Server())
.Columns(columns =>
{
columns.Bound(p => p.Priority)
.Title("Priority")
.HtmlAttributes(new { style = "text-align:left" })
.Width(50)
.Filterable(false)
.Sortable(true)
.Groupable(true) // I can't tell it group and sort it descending.
.GroupHeaderTemplate(#<text>
.Groupable(grouping => grouping.Groups(gr =>
{
//Here I can tell it that I want to sort it descending
gr.Add("Priority", typeof(Boolean), System.ComponentModel.ListSortDirection.Descending);
}))
Please help me or give me a hint on how to fix this issue?
Adding a client side event fixed the issue, it was the reason why the grid was always showing null value in the JQuery function. The Grouping is still an issue; however, the client agree that they could click on the sort button, and it will sort them properly.
I have an MVC 4 application with a Kendo UI grid that I am using for user management. I display the users information in the grid and have a custom command button that when clicked opens a new page to edit the users information. For various reasons (one being there is too much information to be edited to display in the grid) I need to edit the users on a new page and not with inline or popup or incell editing. My grid looks like this... very simple
#(Html.Kendo().Grid<WebSiteNew.Models.ManageUsersViewModel>()
.Name("grid")
.HtmlAttributes(new { style = "margin-top: 5px" })
.Columns(c =>
{
c.Bound(model => model.UserName);
c.Bound(model => model.FirstName);
c.Bound(model => model.LastName);
c.Bound(model => model.Email);
c.Bound(model => model.Phone);
c.Bound(model => model.Extension);
c.Bound(model => model.JobTitle);
c.Command(com => { com.Custom("Edit").Click("edit"); com.Destroy(); });
})
.Pageable()
.Sortable()
.Selectable()
.DataSource(d => d
.Ajax()
.PageSize(20)
.Model(m => m.Id(i => i.UserName))
.Read(r => r.Action("ManageUsers_Read", "ManageUsers"))
.Destroy(o => o.Action("ManageUsers_Destroy", "ManageUsers"))
))
The issue I have here is that I need to pass the ID of the user that the edit button has been clicked on to the edit screen so that I can bring up the information for the selected user. The only way I know of to get this information is through javascript, something like this in my command button click function...
function edit() {
var grid = $("#grid").data("kendoGrid");
var row = grid.select();
var dataItem = grid.dataItem(row);
var id = dataItem.UserId;
location = "#Url.Action("EditUser")";
}
This gives me the id that I need but I have no way (that I know of) to pass this back to my controller. So my question is... how do I get this information back to the server side? Is there another way to get the information I need or is this just something that is not possible in Kendo UI MVC?
To make this a more general question.. when I look at the Kendo UI documentation and it tells me how to get a value in javascript... how, in general, does one go about using this value in an MVC application when it is needed on the server side? I can't seem to find an alternative "MVC" way to do this in their documentation.
You should form additional parameters to url string:
location = "#Url.Action("EditUser")" + "?id="+id;
Having some odd behavior I'd love some feed back on as I can't seem to find any details on this sort of thing anywhere...
Setup:
Using ASP.NET MVC 4 and VS 2010, I have created a corporate directory application with a search page and a details page that displays a corporate tree based on the search selection.
The search page is very basic and using ajax.beginform it returns a list of people back based on the criteria selected. After clicking on the person you get a "tree" showing that individual, their supervisor and any subordinates.
Problem
The user searches for someone, clicks on their result, sees the tree. When they click the back button (with or without performing any other action on the tree view) they are returned to the search screen. The criteria they entered is there but the results are not (as I would expect). If they click search or hit the enter key, the user then receives a 404 page not found error...
What I've Tried
From the things I've read, I've been focusing on possible solutions revolving around the cache, I've tried expiring the page or setting the page for no-cache but that has not seemed to resolve the issue.
CODE
Search View:
#using (Ajax.BeginForm("PersonList", "Home", new AjaxOptions { UpdateTargetId = "results", InsertionMode=InsertionMode.Replace }))
{
#Html.LabelFor(model => model.LastName)
#Html.TextBoxFor(model => model.LastName)<br />
#Html.LabelFor(model => model.FirstName)
#Html.TextBoxFor(model => model.FirstName)<br />
<input type="submit" value="Search" />
<div id="results">
</div>
}
Block that gets plopped in for the search results. This is a partial view that is reused for the tree view as well. That is why you'll see that the link at the top of the block is in a conditional. If the link is to be used from the search page, it's an html link that forces a full page change. If you're already on the tree view though, navigation is done via ajax. The 404 error I'm receiving occurs without navigating at all on the tree view page:
<div id="p#{ Html.DisplayFor(model => model.Id); }" class="personCard">
<img class="personImg" id="img#{ Html.DisplayFor(model => model.Id); }" src="#Url.Content("~/Photos")/#Html.DisplayFor(model => model.Id)" />
<div>
#if(ViewBag.IsForSearch == null) {
#Ajax.ActionLink(this.Model.FullName, "PersonHierarchy", new { id = this.Model.Id }, new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "tree" })
} else {
#Html.ActionLink(this.Model.FullName, "Person", new { id = this.Model.Id })
}
#if(this.Model.TeamSize > 0) {
#: (
#Html.DisplayFor(model => model.TeamSize)
#:)
}
<br />
#Html.DisplayFor(model => model.Title)<br />
#Html.DisplayFor(model => model.MailLocationCode), #Html.DisplayFor(model => model.WorkNumber) #Html.DisplayFor(model => model.ExtensionForDisplay)<br />
#if (this.Model.CellNumber.Trim() != string.Empty)
{
#:Cell:
#Html.DisplayFor(model => model.CellNumber)
#:<br />
}
#Html.DisplayFor(model => model.EmailAddress)
</div>
</div>
Response from Server:
Server Error in '/' Application.
The resource cannot be found. Description: HTTP 404. The resource you
are looking for (or one of its dependencies) could have been removed,
had its name changed, or is temporarily unavailable. Please review
the following URL and make sure that it is spelled correctly.
Requested URL: /
-------------------------------------------------------------------------------- Version Information: Microsoft .NET Framework Version:4.0.30319;
ASP.NET Version:4.0.30319.272
EDIT:
This appears to be specific to IE 9 (in all modes). This behavior does not happen in Chrome and the search form resubmits perfectly after using the back button...
OK...I seem to have found the answer:
To recap, specifically in IE 9 when using ASP.NET MVC Ajax, using the back button to navigate back to a page that has a ajax form on it and trying to submit that form results in a 404 error.
I initially looked into various cache solutions to try to force IE to realize that the page needed to be "reloaded" (even though Chrome and other browsers had no issue with making the form work again when using the back button). This apparently was the correct path to go down but unfortunately I didn't get far enough. The solution ends up being setting the OutputCache for the controller action (or to the entire controller) to:
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
Adding that annotation to the action in the controller or to the controller class as a whole resolved the problem...thanks IE...
For further information on this, I refer to my source:
http://dougwilsonsa.wordpress.com/2011/04/29/disabling-ie9-ajax-response-caching-asp-net-mvc-3-jquery/
Thanks to everyone who tried to help.
Have you tried using developer tools [F12] , Fiddler, Firebug, etc to see what the request is that is returning the 404 error message? By determining what request is returning the 404 you can probably figure out why the request is incorrect.