razor

Razor web pages: How do I create variable that is available to all pages?

人走茶凉 提交于 2020-01-05 08:03:43
问题 I have a lot of pages that is using the same variables. I would like to create these in a single page so I dont have to type them over and over again. How do I do this? I tried both _appStart.cshtml and _PageStart.cshtml, but when i run my content page i get error "the name selectedData does not exist in the current context" Here is my _PageStart.cshtml: (tried _appstart.cshtml too) @{ var db = Database.Open("razortest"); var selectQueryString = "SELECT * FROM tbl_stasjon ORDER BY nr"; var

MVC 4 Validation firing, but the form is still submitting?

帅比萌擦擦* 提交于 2020-01-05 07:25:10
问题 My MVC 4 validation is working as it seems, the error messages pop up, but it still submits the form. Any idea what I am missing? Thank you. public class StatusRequestModel { [Required] [Display(Name = "Ticket Number")] public int? TicketNumber { get; set; } [Required] [Display(Name = "Postal Code")] public int? PostalCode { get; set; } } @using (Html.BeginForm()) { @Html.ValidationSummary() @Html.LabelFor(x => x.TicketNumber) @Html.TextBoxFor(x => x.TicketNumber) @Html.LabelFor(x => x

Razor form with editable collection using partial with Knockout template integration

你说的曾经没有我的故事 提交于 2020-01-05 07:00:10
问题 So, the title is a little contrived, but this is essentially what I want to accomplish: <ul data-bind="template: { name: 'ItemFormTemplate', foreach: Items }"> @foreach (var item in Model.Items) { Html.RenderPartial("_ItemForm", item); } </ul> <script type="text/html" id="ItemFormTemplate"> @{ Html.RenderPartial("_ItemForm", new Item()); } </script> The idea here is that: I want the form for Item to be a partial and I want to use the same partial for both Razor's foreach and feeding Knockout

ServiceStack Razor with Multiple SPAs

不羁的心 提交于 2020-01-05 06:26:27
问题 I don't see an example of using ServiceStack Razor with Multiple SPAs on the internet. The reason for having multiple SPAs in my use case is because my entire site is quite huge and I would like to modularize my site with multiple SPAs. I am aware of the FallbackRoute attribute but it seems to only allow one FallbackRoute based on the documentation? I would like to have, for example, these routes in my app that routes to their respective SPA www.mydomain.com/spa1/... www.mydomain.com/spa2/...

How can I set a select element's itemindex to -1?

只愿长相守 提交于 2020-01-05 05:55:10
问题 I'm dynamically adding values to a select element in my View code like so in my View/.cshtml: @model WebAppRptScheduler.Models.HomeModel @using System.Data @{ DataTable dtUnits = Model.Units as DataTable; var units = from x in dtUnits.AsEnumerable() select new { unit = x.Field<string>("unit") }; ..... <select class="form-control, dropdown" name="unitsselect"> @foreach (var field in units) { <option id="selItem_@(field.unit)" value="@field.unit">@field.unit</option> } </select> ..... The

How can I create a global menu for my application whith its items pulled from a database?

杀马特。学长 韩版系。学妹 提交于 2020-01-05 05:55:10
问题 I need to put couple drop down menus on the upper right hand side of my application. These menus need to appears on every page where that layout is used. The only problem is that items of the menu are pulled from a database. Usually I would pass the list to the model like so public ActionResult Clients() { using (SomeContext db = new SomeContext()) { var clients = db.Database.SqlQuery<Client>("SELECT * FROM clients").ToList(); return View(clients); } } But, I am not sure how to do the same

Change default DateTime format for razor

冷暖自知 提交于 2020-01-05 05:52:52
问题 Is there a way I can change the default formatting for all DateTime objects to e.g. "dd.MM.yyyy" in Razor ? I would like to cover cases when a programmer forgets to call an extension method or to pass a format string, i.e. <p>@Model.MyDateTimeObject</p> . 回答1: There is no thing like default formatting in Razor . When you do this: <p>@Model.MyDateTimeObject</p> Razor will simply use the default DateTime.ToString overload to print the date. This works by the actual culture information, so for

Add one day to a model's data

℡╲_俬逩灬. 提交于 2020-01-05 05:51:14
问题 I am new to Razor and I want to know how to add one day to model's date object. I need to put a "next day" link Here is the view code <a href="@Html.DisplayNameFor(model => model.date)">Next Day</a> 回答1: You can pass the NextDay as a DateTime in the Model as well using DateTime.AddDays() method. This method returns a new DateTime that adds the specified number of days to the value of this instance. model.NextDay = startDate.AddDays(1); then use that property in your DisplayNameFor : <a href="

Data Annotation Validation not working in ASP.NET MVC 3

我的梦境 提交于 2020-01-05 05:24:05
问题 I Have a form inside a Partial view that is rendered inside a tab on a tab control that looks like this: @model USARAFSyncMVC.Areas.Event.Models.EventFullScaffoldModel @using (Ajax.BeginForm("SaveMainEventDetails", "Event", new { area = "Event" }, new AjaxOptions { UpdateTargetId = "FormWrapper", OnComplete = "SetSuccessLabel", InsertionMode = InsertionMode.Replace }, new { method = "post" })) { @Html.Hidden("eventType", "1", new { id = "eventType" }) <div id="FormWrapper"> <hr /> <table

How to display only one error message using Html.ValidationSummary?

拟墨画扇 提交于 2020-01-05 04:57:12
问题 I have a view (skipping redundant code for simplicity): @model CompetencyModel @using (Html.BeginForm()) { @Html.ValidationSummary() @Html.EditorFor(model => model.Results) } When I submit, if validation fails inside EditorFor , ValidationSummary shows the list of all validation messages for each item coming from EditorFor . This screenshot is a view, where EditorFor is a single line with radio buttons: But I need a single error message just saying that something failed. I tried: @Html