FAQ Database Discussion Community


ASP.NET MVC TextArea doesn't respect row parameters

c#,html,asp.net-mvc,razor
I have the following TextArea which contains formatted XML: @Html.TextArea("ConfigXmlView", System.Xml.Linq.XDocument.Parse(Model.IdentifiFIConfiguration.ConfigXml).ToString(), 20, 50, null) The row parameter (20 in this case) seems to not affect the actual height of the text area at all. What could the problem be? And is there a way to automatically set the height of...

How to hide role based section in knockout component using asp.net MVC

javascript,asp.net,asp.net-mvc,razor,knockout-3.0
What is the best way to prevent user from seeing things like admin links in a knockout component? I don't want to make a client request if the user has right to see those links because it would expose this section on the client. The only way I can figure...

Does HTML Encoding have any cons?

asp.net-mvc,razor,encoding,utf-8,xss
I develop a project on ASP.NET MVC framework. All files and charsets are in UTF-8. I'm using model bindings and in some of my models the display property includes some accented chars or single/double quotes. As Razor engine automatically encodes helpers (ie. DisplayNameFor) the accented chars and quotes are encoded....

Display a tooltip with Html helper

html,asp.net,.net,asp.net-mvc,razor
I'd like add a tooltip to my razor view like this : @Html.Editor("Password", new { Title = "Doit contenir au moins une lettre majuscule, une minuscule, un chiffre et un caractère spécial" }) It is not working, If I just change the html helper by the input html tag, it...

How to Implement Dependent Dropdownlist in MVC4 Razor and using SQL server also

sql-server,asp.net-mvc-4,razor
I've 2 Dropdownlist called States and Country. but only the States DDL(DropdownList) will be enable the country DDL will be diable. when I select a option in States which is already stored in SQL. The Country DDL Should Show the Selected state's Country automatically from DB. Please help Me..... Ex:If...

Disable select2 dropdown with knockout binding?

javascript,jquery,asp.net-mvc,razor,knockout.js
I have an MVC Razor project and in my view i have one Html.TextBoxFor with data-bind="disable: setRoot". This input is used by Select2 jquery plugin. My problem is that the disable binding is not working properly. This is my code: <div class="control-label">Category</div> <div class="input-group"> <div class="input-group-addon"><label><input data-bind="checked: setRoot" type="checkbox"> root</label>...

Strange situation, Visual Studio debugging ran through action to view but nothing returned to browser

c#,jquery,ajax,asp.net-mvc,razor
I have encountered a very strange problem. Basically, there is a Delete ActionLink. Once clicked, the code checks the condition, checks true/false, stays on current page or goes to Delete view. I have posted the solution for this scenario and got some very helpful replies. I worked on it and...

Why does my MVC binding stop working when I assign a nested model to a variable?

c#,asp.net-mvc,asp.net-mvc-4,razor
I have code something like this contrived example in my MVC 4 razor view: @for (int i = 0; i < Model.NestedModel.Count; i++) { @Html.HiddenFor(model => modelModel.NestedModel[i].Id) @Html.EditorFor(model => Model.NestedModel[i].SomeProperty) } In which case everything works fine for me However, I wanted to tidy this up a little as the...

MVC View trying to parse 2 strings in Details view

asp.net-mvc,asp.net-mvc-4,razor
its an MVC razor project with entity framework, simple record view/edit/create in View, the details cshtml I want to read out just 2 of the record values and based on their contents, create a label. <h1 @if(Model.unittype == "PRP" && Model.name =="TTO"){string x = "CISCO";}else x = "---";>x</h1> not a...

Kendo UI ASP.NET MVC ListView fails when trying to add or edit items

c#,asp.net-mvc,listview,razor,kendo-ui
I'm struggling to set up a ListView which allows me to add new records and to update existing ones. Loading the items for the list works like a charm. Also showing the only relevant information Id and Line works perfectly. Two problems: 1. Adding a new item to the list:...

How do you send data to controller with ajax.beginform?

ajax,asp.net-mvc-4,razor,model-view-controller,model
This is my method on controller "sale" [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(Models.account account) { Models.sale creaventa = new Models.sale(); //creaventa.account = cliente; creaventa.createdon = DateTime.Now; creaventa.idaccount = account.id; creaventa.modifiedon = DateTime.Now; creaventa.status = 0; context.sales.Add(creaventa); context.SaveChanges(); // return "venta creada"; return View(); } and this is the partial view @model...

Identifier starts immediately after numeric literal

jquery,ajax,razor
I'm trying to get the current logged in usersID in my jquery function var userID = @User.Identity.GetUserId(); Many answers on this page say that I have to do something like \"@User.Identity.GetUserId()\" But it dont seem to work, can someone tell me how to fix it. Its for my ajax post....

How do I convert an array from the ViewBag to a JScript array using Razor Syntax? [duplicate]

javascript,c#,arrays,asp.net-mvc,razor
This question already has an answer here: How to return Json object from MVC controller to view 3 answers I am trying to re-code the following razor syntax to be workable code and am stuck: var ServiceIndex = @ViewBag.ServiceID ; var ServiceName = @ViewBag.ServiceName ; var ServiceNotes = @ViewBag.ServiceNotes...

In Javascript in a MVC Razor, replace a keyword with a value from the model

javascript,asp.net-mvc,razor
When you have some JavaScript on an MVC razor view is it possible to do a model variable replacement in the middle of a multipart statement. To clarify I have the code <script> function drawChart() { $.post('@Url.Content("~/Home/GetDataAssets")', function (d) { ... var chart = new google.visualization.LineChart(document.getElementById('chart_div')); chart.draw(data, options); }); };...

DropDownList Change() doesn't seem to fire

javascript,c#,asp.net-mvc,razor
So, I have been bashing my head against the desk for a day now. I know this may be a simple question, but the answer is eluding me. Help? I have a DropDownList on a modal that is built from a partial view. I need to handle the .Change() on...

Prevent New Line Using Razor Conditional Statement

c#,asp.net-mvc,razor
I want to display some html content using the razor syntax, based on some variable contained in the ViewBag object. Current the content breaks to newline after the conditional statement. This is what I have: <h5>Apply salary @ViewBag.salaryStatus to associated objects?</h5> @if (ViewBag.salaryStatus == "Executed") { <h5>(This will overwrite old...

How do I create an editor template that works with existing data?

c#,asp.net,asp.net-mvc,forms,razor
I created an editor template to display text boxes included in a form. @model WebApplication1.Models.PersonViewModel <input type="text" class="form-control" name="@ViewData.TemplateInfo.HtmlFieldPrefix" value=""/> I simply want to include the 'form-control' class in all my input text boxes used to collect data for this model. public class PersonViewModel { [UIHint("_TextFormControl")] public string FirstName {...

Http Post with duplicate name values MVC

c#,asp.net,asp.net-mvc,razor,http-post
In MVC I have a form that uses a lot of duplicate information. Basically they click a button and there is another "form"(text box drop down list etc) that pops up. When they click the submit button however all of that comes back with the same name. How would I...

my model binding is working even If I didn't show it in html

c#,asp.net,asp.net-mvc,razor,model-binding
I have a form that can be reached by different ways. In the URL, if user comes with ?data=johndoe (example), I didn't want to deal with another property, so I didn't show it to user and I don't care of this property. Because I use the same form and the...

Get Column title in View

asp.net-mvc,razor,parameters,title,actionlink
MVC5 did a great job on automatically creating the View page (Razor). It currently displays my list of Students for example with table heading like this: <th> @*@Html.ActionLink("", "Index", new { sortOrder = ViewBag.NameSortParm })*@ @Html.DisplayNameFor(model => model.LastName) </th> Now. DisplayNameFor take a Lambda Expression as its parameter. However, I...

@Html.RadioButtonFor in mvc

c#,asp.net-mvc-4,razor
In my application, my model contains a field id, and in the view I need to select an id with a radio button and post back the selected id to the controller. How can I do this? My view is as follows, @model IList<User> @using (Html.BeginForm("SelectUser", "Users")) { <ul> @for(int...

How to unmask/disable or clear a textbox in JavascriptJquery using MVC razor

javascript,c#,jquery,asp.net-mvc,razor
I have a text box <input type="text" id="mytextbox" name="searchField"/> and three radio buttons. I need to mask text box on select of specific radio button selection. I have successfully done with it. But on third radio button I want text box to just clear without any masking, just an open...

Escaping quotes in Newtonsoft JSON

c#,asp.net-mvc,razor,json.net
I've an object: public class Test { public string Prop1 { get; set; } } I'd like to serialize it to json in a view, so in my cshtml: <script type="text/javascript"> var myJson = JSON.parse('@Html.Raw(JsonConvert.Serialize(Model.MyTest))'); </script> It works, until Prop1 contains quotes, because it gets rendered as: var myJson =...

Set var in foreach to use later

c#,razor
I'm trying to set a value in a foreach but i get the error that it dosent exist in the current contect. Here is my code; @{ string sUsr = System.Web.HttpContext.Current.Request.ServerVariables["AUTH_USER"].ToLower(); var db = Database.Open("SQLServerConnectionString"); var selectUser = "SELECT * FROM USERS WHERE username = " + @sUsr; } @foreach...

Javascript Object negative number

javascript,asp.net-mvc,razor
Create object adjustementType inside script tag in MVC 5 partial view. <script type="text/javascript"> var adjustementType = { -1:'Rate Decrease', 1: 'Rate Increase' }; </script> but getting following error "Expected identifier, string or number" . The error is thrown on area -1 & 1 field....

Jquery Tabs - return selected tab dynamically

c#,jquery,asp.net-mvc,asp.net-mvc-4,razor
On page load I would like to display selected tab dynamically. This is so validation is displayed for the correct tab. I have the following code: <script type="text/javascript"> $(document).ready(function () { var selectedTabId= $("#SelectedTab").val(); alert(selectedTabId); $('#tabs').tabs( { cache: false, beforeActivate: function (event, ui) { selectedTabId = ui.newPanel.attr('id'); $("#SelectedTab").val(selectedTabId); }, selected:...

Is it possible to append html to a class or div using razor syntax

razor,html-helper,asp.net-mvc-views
I am coding a MVC 5 internet application and have a question in regards to accesssing html classes or divs via razor syntax. Is it possible to access a html class or div via razor syntax and append some html code to the class or div? Is there a html...

In MVC Razor, how can I correctly add querystring parameters to an html.actionlink?

asp.net-mvc,razor,asp.net-mvc-5
I have an MVC Razor page where I am trying to add a querystring parameter to the URL. <td> @Html.ActionLink(item.Student_Name, "Index", "FourCourseAuditDetails", new { filterByStudent = item.Student_Name}) </td> My desired outcome is: http://[server]/FourCourseAuditDetails/Index?filterByStudent="[item.Student_Name]", but when I test out the page, the anchor href attirubte looks like this: http://[server]/[route of current...

Is it possible to add views to deployed .NET MVC Razor site

asp.net,.net,asp.net-mvc,razor
Is it possible to add Razor views to a deployed .NET MVC site? How would you do this?

Disable Copy/Paste/Right Click in mvc TextBox

javascript,asp.net-mvc,razor
I have a textbox which accepts numeric value.This is handled using javascript. I'd like to disable copy, paste and right click functionalities for the text box. Any help would be appreciated. @Html.TextBoxFor(model => model.Days, new { @class = "input_box", @id = "txtDays", @onkeydown = "javascript:NumberOnly(this,event)"}) ...

A potentially dangerous Request.Path in ASP.NET AjaxActionLink Request

c#,asp.net-mvc,razor
I have a cshtml view in an MVC ASP.NET application. I want to simply allow the user to click a button and provide a warning to the user before the action is carried out. The only way I know to acheive what I want is to use an AjaxActionLink like...

Binding my view model to a view causes all check boxes to be checked

c#,asp.net,asp.net-mvc,asp.net-mvc-4,razor
I'm having an issue where my all the check boxes rendered in the view are coming out checked. I put a breakpoint at the line where my view model is constructed and through the debugger, I can see that some values are set to "true" and others are set to...

How to use more than 1 Model on a page by using asp.net mvc 5?

c#,razor,asp.net-mvc-5
I want to use 2 models. The first is on Index.cshtml page, and the second is on _Layout.cshtml page In the controller which contains the action public ActionResult Index(){...}, I declare some values and return it to View(). Like this: public ActionResult Index() { HomePageViewModel model = new HomePageViewModel(); //...

Return to the same view when no image is selected

asp.net-mvc-4,razor,json.net,entity-framework-5,c#-5.0
I have an upload control. But if the user didnt select an image and the press on the upload button, the user will get an message that he/she has to go back and try again, like this: if (isSavedSuccessfully) { return Redirect(Url.Action("Edit", "Account") + "#tabs-2"); } else { return Json(new...

jQuery: How to traverse / Iterate over a list of object

javascript,jquery,asp.net-mvc,asp.net-mvc-4,razor
I'm using asp.net MVC4 for web app development. I would like to traverse a list of objects from a ViewModel. Below is the class of the object: public class User { public int Id {get; set;} public string Name {get; set;} public string Address {get; set;} public string Department {get;...

Are intranet sites vulnerable to CSRF?

asp.net,asp.net-mvc,razor,asp.net-mvc-5
I have developed and deployed an MVC5 .NET app which runs within an intranet and uses LDAP to authenticate users. Since MVC 5 gives you the @Html.Antiforgery() by default I used them in every from. However in production where the app is running in multiple nodes I'm having problems with...

How to assign C# variable to CSS class variable in ASP.NET MVC ActionLink

c#,css,asp.net-mvc,razor,dynamic-css
In the ActionLink code below, how can I assign a C# string variable to @class in the view in ASP.NET MVC 5? @Html.ActionLink("Manage List", "Index", new { @class = "DynamicClassName" }); I want to replace the static string @class = "DynamicClassName" with something dynamic, similar to @class = @myChangingColorClass //...

ListBox that updates another part of View

c#,html,asp.net-mvc,razor,listbox
I want a ListBox, that when submitted, will send its selected values to a controller, and the response from that controller will be used to update a small piece of the view. The following code is able to update the part of the view, and get the selected values, but...

Take the first 100 characters from content MVC 5 ASP.NET

c#,asp.net,asp.net-mvc,razor
I am using asp MVC 5 with entity framework 6.1 code first. I made a view for the articles. I want to take only the first 100 characters from the content to put it in the index view. How can I do it ? <td> @Html.DisplayFor(modelItem => item.Content) </td> ...

Output Raw String in ASP.NET MVC Razor [duplicate]

c#,asp.net-mvc,razor
This question already has an answer here: Emitting unencoded strings in a Razor view 4 answers I have a site built with ASP.NET MVC. I have a string in my view model that looks like this: ViewBag.Text = "{\"1\":{\"1\":\"John\",\"2\":\"Bill\",\"3\":\"Paul\"},\"3\":{}}"; I want to output this into my view like this:...

Iterating over an ArrayList Always misses 1 entry

c#,asp.net,razor
When iterating over an ArrayList of strings to display on my WebForm using Razor one entry is never actually displayed. The code: <asp:UpdatePanel ID="TweetUpdatePanel" runat="server"> <ContentTemplate> <% ArrayList alList = GetLatest(); %> <ul> <% foreach (string tweet in alList) { %> <li> <asp:Literal ID="thisLiteral" runat="server"></asp:Literal> <% thisLiteral.Text = tweet; %>...

Jquery: Change contents of