razor

DisplayNameFor() From List<Object> in Model

☆樱花仙子☆ 提交于 2019-12-17 04:01:33
问题 I believe this is pretty simple, I just can't seem to find the right way to show the display name for an item within a list within my model. My simplified model: public class PersonViewModel { public long ID { get; set; } private List<PersonNameViewModel> names = new List<PersonNameViewModel>(); [Display(Name = "Names")] public List<PersonNameViewModel> Names { get { return names; } set { names = value; } } } and Names: public class PersonNameViewModel { public long ID { get; set; } [Display

Determine if uploaded file is image (any format) on MVC

你离开我真会死。 提交于 2019-12-17 03:52:00
问题 So I'm using this code for view: <form action="" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <input type="submit" /> </form> This for model: [HttpPost] public ActionResult Index(HttpPostedFileBase file) { if (file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName); var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName); file.SaveAs(path); } return RedirectToAction("Index"); }

How to render a Section in a Partial View in MVC3?

两盒软妹~` 提交于 2019-12-17 03:34:05
问题 In a MVC3 project, I have a "_Layout.vbhtml" file with this code <!DOCTYPE html> <html> <head> </head> <body> ... <script src="@Url.Content("~/Scripts/jquery-1.8.2.min.js")"></script> @RenderSection("Scripts", false) </body> </html> Then, I have a Partial View "ValidationScripts.vbhtml" in the Shared folder with @Section Scripts <script src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script> <script

asp.net-mvc: razor '@' symbol in js file

旧巷老猫 提交于 2019-12-17 03:31:06
问题 I have a .csHtml -razor file with a javascript function that uses the @Url.Content C# function inside for the ajax URL. I want to move that function to a .js file referenced from my view. The problem is that javascript doesn't "know" the @ symbol and doesn't parse the the C# code. Is there a way to reference .js files from view with "@" symbol? 回答1: You could use HTML5 data-* attributes. Let's suppose that you want to perform some action when some DOM element such as a div is clicked. So:

ASP.NET MVC Razor pass model to layout

妖精的绣舞 提交于 2019-12-17 03:24:32
问题 What I see is a string Layout property. But how can I pass a model to layout explicitly? 回答1: Seems like you have modeled your viewmodels a bit wrong if you have this problem. Personally I would never type a layout page. But if you want to do that you should have a base viewmodel that your other viewmodels inherits from and type your layout to the base viewmodel and you pages to the specific once. 回答2: Add a property to your controller (or base controller) called MainLayoutViewModel (or

ASP.NET MVC 3 Razor: Include JavaScript file in the head tag

人走茶凉 提交于 2019-12-17 03:21:49
问题 I'm trying to figure out the proper Razor syntax to get a JavaScript file for a particular *.cshtml to be in the head tag along with all the other include files that are defined in _Layout.cshtml. 回答1: You can use Named Sections. _Layout.cshtml <head> <script type="text/javascript" src="@Url.Content("/Scripts/jquery-1.6.2.min.js")"></script> @RenderSection("JavaScript", required: false) </head> _SomeView.cshtml @section JavaScript { <script type="text/javascript" src="@Url.Content("/Scripts

Return View as String in .NET Core

∥☆過路亽.° 提交于 2019-12-17 03:00:13
问题 I found some article how to return view to string in ASP.NET, but could not covert any to be able to run it with .NET Core public static string RenderViewToString(this Controller controller, string viewName, object model) { var context = controller.ControllerContext; if (string.IsNullOrEmpty(viewName)) viewName = context.RouteData.GetRequiredString("action"); var viewData = new ViewDataDictionary(model); using (var sw = new StringWriter()) { var viewResult = ViewEngines.Engines

Add CSS or JavaScript files to layout head from views or partial views

眉间皱痕 提交于 2019-12-17 02:52:23
问题 Layout pages head: <head> <link href="@Url.Content("~/Content/themes/base/Site.css")" rel="stylesheet" type="text/css" /> </head> A View (AnotherView) from the application needs: <link href="@Url.Content("~/Content/themes/base/AnotherPage.css")" rel="stylesheet" type="text/css" /> and AnotherView has a partial view (AnotherPartial) which needs: <link href="@Url.Content("~/Content/themes/base/AnotherPartial.css")" rel="stylesheet" type="text/css" /> Question: How can we add these CSS files

dropdownlist set selected value in MVC3 Razor

﹥>﹥吖頭↗ 提交于 2019-12-17 02:43:10
问题 Here is my model: public class NewsCategoriesModel { public int NewsCategoriesID { get; set; } public string NewsCategoriesName { get; set; } } My controller: public ActionResult NewsEdit(int ID, dms_New dsn) { dsn = (from a in dc.dms_News where a.NewsID == ID select a).FirstOrDefault(); var categories = (from b in dc.dms_NewsCategories select b).ToList(); var selectedValue = dsn.NewsCategoriesID; SelectList ListCategories = new SelectList(categories, "NewsCategoriesID", "NewsCategoriesName"

ASP.NET MVC 3 - Partial vs Display Template vs Editor Template

送分小仙女□ 提交于 2019-12-17 02:30:22
问题 So, the title should speak for itself. To create re-usable components in ASP.NET MVC, we have 3 options (could be others i haven't mentioned): Partial View: @Html.Partial(Model.Foo, "SomePartial") Custom Editor Template: @Html.EditorFor(model => model.Foo) Custom Display Template: @Html.DisplayFor(model => model.Foo) In terms of the actual View/HTML, all three implementations are identical: @model WebApplications.Models.FooObject <!-- Bunch of HTML --> So, my question is - when/how do you