razor

Should one try to guard against null reference exceptions / index out of bounds exceptions in MVC views?

旧巷老猫 提交于 2019-12-23 18:01:22
问题 I want to post a comment to this question's accepted answer, " Haven't views abandoned code behind now? So what are you going to test? " pointing out that it seems to me as soon as you add an <% if (Model.Thing == "abc") {} %> or @if (Model.Thing == "abc") {} to your view, there exists the potential for something to blow up, and that potential should be guarded against. In regard to the question I linked to, I could see an argument being made that one should guard against the possibility of

Global functions in razor view engine

坚强是说给别人听的谎言 提交于 2019-12-23 17:48:35
问题 I want to have a global method like w in my razor view engine for localization my MVC application. I tried @functions{ public string w(string message) { return VCBox.Helpers.Localization.w(message); } } but I should have this in my every razor pages and I don't want that. I want to know how can I have a global function that can be used in every pages of my project? 回答1: How about an extension method: namespace System { public static class Extensions { public static string w(this string

RenderPartial, model as argument

你。 提交于 2019-12-23 17:24:32
问题 I get a type error but I don't understand why, I have a View, a ViewModel, a PartialView and a Model. The view gift has giftViewModel as model. giftViewModel contain an instance of the Model LoginModel (login). The partialView LoginPopUp takes a LoginModel as model. I try to render the partialView LoginPopUp within the gift view, passing it login as model. And I get this error : The model item passed into the dictionary is of type 'GiftViewModel', but this dictionary requires a model item of

TextBoxFor decimal

白昼怎懂夜的黑 提交于 2019-12-23 17:03:11
问题 In my database I stored fields with the data type decimal . I am using exactly the same ( decimal ) data type in my ASP.NET application. This is inside my view in order to display the value. @Html.TextBoxFor(model => model.Stock, new { id = "Stock", @class = "k-textbox" }) This pretty straight forward. The only issue I am facing is that by default in my view the data is displayed with 4 decimal places. I give you a few examples on how it looks and how it should look: 1,0000 => 1 1,2000 => 1,2

ServiceStack Razor (self-hosted) with embedded images/css

半世苍凉 提交于 2019-12-23 16:55:07
问题 I have a self-hosted WebService/WebApplication using the wonderful Service Stack. My views are embedded in the DLL, and so are the images. I am using the ResourceVirtualPathProvider code from GitHub. It finds the index page and layouts correctly, but it can't find the embedded images/css (probably obviously). How would I go about configuring the Razor plugin to search the path providers. I've checked in debug mode and the path providers have found all the css and images. They're just not

Defining custom template for kendo ui grid column

ぐ巨炮叔叔 提交于 2019-12-23 16:43:20
问题 I have a kendo ui grid and I want to bind images. Here is my code: @model List<NewHope.Model.Mt4_prices_instant> <div class="tabContainer"> @(Html.Kendo().TabStrip() .Name("tabstripMarketWatch") .Items(tabstrip => { tabstrip.Add().Text("Market Rates") .Selected(true) .Content( @<text> @if (Model != null) { @(Html.Kendo().Grid(Model) .Name("Grid") .Columns(columns => { columns.Template( @<text> @if (item.direction == 1) { <img src="~/Images/up.png" alt="up"/> } else if (item.direction == 0) {

MVC 3 - FormsAuthentication - Can't give access to my Login action

与世无争的帅哥 提交于 2019-12-23 15:37:16
问题 I'm using MVC 3 and FormsAuthentication: <authentication mode="Forms"> <forms loginUrl="~/Account/" timeout="2880" /> </authentication> My Account/Index page displays a login page, then using Jquery, it does a post to Account/Login to authenticate the user. Here's my AccountController action which handles it: [HttpPost] [Authorize(Users = "*")] public ActionResult Login(string userName, string password) { ... } And the Jquery which posts to it: $.post("/account/login", { "userName": $("

looping through model with razor

微笑、不失礼 提交于 2019-12-23 15:24:49
问题 Trying to just print out the name of each movie item. eg. The Dark Knight Am I missing some declarative statement in the razor code? Intellisense doesn't list any options after Model [XmlRoot("movies")] public class MovieSummary { [XmlElement("movie")] public List<Movie> Movies { get; set; } } public class Movie { public int id { get; set; } public string name { get; set; } } public ActionResult Index() { MovieSummary summary = Deserialize(); return View(summary); } public static MovieSummary

Unobtrusive validation attributes rendered for nullable DateTime

非 Y 不嫁゛ 提交于 2019-12-23 13:26:11
问题 I have this setup: ViewModel: public class MyViewModel { public DateTime? Start { get; set; } } View: @model MvcApplication2.Models.MyViewModel @using (Html.BeginForm()) { @Html.EditorFor(x => x.Start) } Resulting HTML: <input class="text-box single-line" data-val="true" data-val-date="The field Start must be a date." id="Start" name="Start" type="datetime" value="" /> This HTML includes the unobtrusive validation attributes. Why is that? I was under the impression that there would be no

Using HTML.TextBoxFor to loop through items in a model [duplicate]

风流意气都作罢 提交于 2019-12-23 13:06:48
问题 This question already has answers here : Post an HTML Table to ADO.NET DataTable (2 answers) Closed 3 years ago . Probably a stupid question but I am new to MVC. So far in my Razor I could say @HTML.TextBoxFor(t => t.EmailAddress) but now I have a for-each: foreach(var q in Model.Questions) { // so here the t => t.EmailAddress syntax is not working anymore. } I asked my question in the code sample above. So when I am inside a for-each loop how can I can use @HTML.TextBox ? because now it