FAQ Database Discussion Community


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; %>...

How to run javascript function in Razor MVC .net

javascript,asp.net-mvc,asp.net-mvc-4,razor
<script type="text/javascript"> @if (@Model.Invoice.InvoiceDate != null) { <text> function UTCToLocalTime(date) { var time = new Date(Date.parse(date)); var timeOffset = -((new Date()).getTimezoneOffset() / 60); time.setHours(d.getHours() + timeOffset); return time; } var date = @Model.Invoice.InvoiceDate UTCToLocalTime(date); </text> } </script> I am printing the above invoice and its open in a window....

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 =...

Transform Querystring to RouteValue in Razor View

c#,jquery,html,asp.net-mvc,razor
I'm stuck with a very basic detail in a view. I want to be able to let the user filter the results in the Index view. To do this I've created a dropdown list, which gets populated thourgh my viewmodel: @using (Html.BeginForm("Index", "Captains", FormMethod.Get)) { <div class="row"> <div class="dropdown"> @Html.DropDownList("Name",...

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

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

Render different tables in a view based on years

c#,asp.net-mvc,html5,razor
I am trying to make a view that renders different html tables for different years. The model used for the view is this: public class VisualizzaFattureViewModel { public FileInfo FatturaFile { get; set; } public int Year { get; set; } } And this is the controller action: public ActionResult...

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

Using models correctly in partial razor views

c#,asp.net-mvc,razor
I have the following controller: public class WelcomeController { private IWelcomeService _service; public WelcomeController(IWelcomeService service) { _service = service; } public override ActionResult Index() { WelcomeIndexViewModel viewModel = new WelcomeIndexViewModel(); viewModel.LatestNewsArticles = _service.GetLatestNewsArticles(); return View(viewModel); } } And the following is the view model: public class WelcomeIndexViewModel { public IEnumerable<LatestNewsArticle>...

How to iterate over @Model.ICollection in Java Script function?

javascript,.net,asp.net-mvc,razor,icollection
I have a @Model.LoginCoordinates where LoginCoordinates is ICollection<Coordinates>. The Coordinates class: public class Coordinates { public int Id { get; set; } public double Latitude { get; set; } public double Longitude { get; set; } } Inside the view in I have: <script> function f(){ for (var i =...

404 Not Found Ajax jquery MVC

c#,jquery,ajax,asp.net-mvc,razor
I created an MVC3 project in Visual Studio 2013. The view engine is razor.First I write jquery.ajax in @section Scripts {} in view(cshtml) it works fine. But I seperate script in .js file and debugging I get error : (debugging IIS Express) IIS 8.0 Detailed Error - 404.0 - Not...

Getting and passing MVC Model data to AngularJS controller

javascript,asp.net-mvc,angularjs,razor,asp.net-mvc-5
Hey guys so I'm pretty new to AngularJS and I'm at a loss here. Right now my MVC program uses Razor to display all the data in my .mdf database (i.e: @Html.DisplayFor(modelItem => item.LastName) ). However, I want to go mostly Angular. I am trying to use ng-repeat to display...

Cannot access conditional Razor variable as href

c#,asp.net-mvc,razor
I have a razor view that generates a url based on some items returned by the model. When I create a url variable without any if statements, the url variable can be found and used as an href. However, when I have this code (using if statements) it says: "The...

Selecting Multiple Linq Fields Removing Column Names

c#,linq,razor
I need to select multiple fields into an enumerator to run a foreach in my Razor web app. In the controller, I have: ... cols = (from b in a.RefTable select new {b.Col1,b.Col2,b.Col3}), ... It returns the values correctly when I use: @foreach(var col in @item.cols) { @col } However,...

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;...

Why HTML form empty field auto check disabled when false is returned from JS function on click of submit button? [duplicate]

javascript,jquery,html,forms,razor
This question already has an answer here: Triggering HTML5 Form Validation 4 answers If an HTML form contains an input text field and it is declared like this: <input type="email" id="loginEmail" placeholder="Email address" required> Then the if this field is empty and user clicks on submit button then an...

Request.Form is null on making button as disabled

c#,asp.net-mvc,razor
I have two buttons on my screen i am handling with one method : Request.Form["Approve"] becomes null as i have disabled the button, how i can get the value of Request.Form["Approve"] after disabling the button. @if(ViewBag.RequestAlreadyProcessed) { <input type="submit" class="btn btn-primary btn_box" value="Approve" name="Approve" disabled="disabled" /> <input type="submit" class="btn btn_secondary...

Return to second to last URL in MVC (return View with previous filter conditions applied)?

c#,asp.net-mvc,razor,asp.net-mvc-5,grid.mvc
I'm working on an MVC5 application. On the home screen is a grid allowing users to view Data and be transferred to a number of Views for various actions on each record. One of these is an [EDIT]. The issue I'm encountering is as follows: due to the amount of...

how to get the text inside using jquery if a contain more than one value

jquery,asp.net-mvc,razor
I have a table which will create dynamically during a button click <table id="diagnosis" style="display:none"> <tr> <td> <input id="diag-%" class="diag" style="width:200px" type="text" name="provider_diagnosis_dtls[#].diagnosis_code" value /> </td> <td> <input id="desc-%" class="diag_desc" style="width:500px" type="text" name="provider_diagnosis_dtls[#].diagnosis_desc" value /> </td> <td> <input id="level-%" type="text" name="provider_diagnosis_dtls[#].diagnosis_level" readonly...

Get value off querystring by it's position

c#,asp.net,razor
I have a URL that's going to have one parameter on it but the 'name' of this parameter will, in some cases, be different eg. www.mysite.com/blog/?name=craig www.mysite.com/blog/?city=birmingham and what I'm trying to do is always get the value (craig / birmingham) of the first (and only) parameter on the string...

Can't get Editor Template to Work

jquery,asp.net-mvc,razor
I'm trying out editor templates and I can't get it work. I'm working off of this tutorial to try and get a jQuery drop calendar to appear for a DateTime? Can anyone see what I'm doing wrong? I changed my code and tested a TextboxFor and it worked out so...

@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...

ServiceStack View 403 (Forbidden)

razor,servicestack,servicestack-razor
I have setup Service Stack web project with a couple of views. I can access the /Default.cshtml view without any problems but when I try to access anything in the /Views/ folder I get the below error: Forbidden Request.HttpMethod: GET Request.PathInfo: /Views/MyView.cshtml Request.QueryString: Request.RawUrl: /Views/MyView.cshtml I have looked at the...

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

foreach() over List in ViewBag recognizes T as object instead of actual type

c#,asp.net-mvc,razor
I am getting this strange error where Razor would say a property does not exist within an object. I sense I am doing a silly mistake, but I've been battling with this for a while but no luck. Any clues? Controller method: List<Conversations> lConversations = usr.conversations.OrderBy(o => o.active).ThenByDescending(o => o.updated_at).ToList().Select(i...

ASP.NET MVC DropDownList NullReferenceException

c#,asp.net-mvc,razor
Im having a problem with my dropDownList giving me a NullReferenceException and I don't know why. I want to post a post where you can choose with the DropDownList which Category it belongs to. Here is some of my code: PostModel public class Post { [Key] public int PostID {...

Jquery: Change contents of