razor

What is ViewBag.Title in Razor?

廉价感情. 提交于 2019-12-20 16:28:33
问题 What is ViewBag.Title in ASP.NET MVC 4? I have that View file: @model IEnumerable<MvcMusicStore.Models.Genre> @{ ViewBag.Title = "Store"; } <h2>Index</h2> and I do not know what changing ViewBag.Title may accomplish. 回答1: From the ASP.NET site: ViewBag is a dynamic object, which means you can put whatever you want in to it; the ViewBag object has no defined properties until you put something inside it. The ViewBag.Title property is simply a string object. In this case it's being used in the

Access displayName attribute from the model in MVC View

泪湿孤枕 提交于 2019-12-20 16:21:17
问题 If my model have [DisplayName("First Name")] public string firstName { get; set; } Then I can print it in the View with LabelFor @Html.LabelFor(model => model.acc_first) It will then render as <label for="firstName">First Name</label> But how can I "raw" read the property (FirstName) attributes in the View? If for example, I want to send the value to a function on the View page 回答1: @Html.DisplayNameFor(x => x.acc_first) 回答2: To access the attributes you'll need a custom Html helper. Since

when passing a collection to EditorFor(), it generates invalid names for input elements

房东的猫 提交于 2019-12-20 14:19:20
问题 I have a BookCreateModel which consists of book's plane info such as Title, PublishYear & etc plus a collection of book Authors (complex type) : public class BookCreateModel { public string Title { get; set; } public int Year { get; set; } public IList<AuthorEntryModel> Authors { get; set; } } public class AuthorEntryModel { public string FirstName { get; set; } public string LastName { get; set; } } in CreateBook view I have used EditorFor helper : @Html.EditorFor(m => m.Authors,

ASP.NET MVC3 AntiForgeryToken

吃可爱长大的小学妹 提交于 2019-12-20 14:15:26
问题 Here I have simple MVC3 application with two form posts. To protect CSRF attack, I have used antiforgerytoken html helpers in both forms as per guidance here. Here are my two models: public class User { public string FirstName { get; set; } public string LastName { get; set; } } public class Employee { public int Id { get; set; } public string Name { get; set; } } Here is my homeController.cs: public class HomeController : Controller { public ActionResult Index() { return View(); } [HttpPost]

Looping through models content in Razor

夙愿已清 提交于 2019-12-20 13:22:59
问题 I want to loop through each item in my model in my razor view but I want to group all items together. I then want to loop through each group. Imagine I have a table: ID GroupNo GroupName 1 1 Group1 2 1 Group2 3 1 Group3 4 2 Group1 5 2 Group2 6 3 Group56 I want to do something like: @foreach (var group in Model.GroupNo) { <section> <header>Group No is @group.GroupNo</header> @foreach (var item in group) { <p>GroupName: @item.GroupName</p> } </section> } So my output is: Group No is 1 GroupName

Looping through models content in Razor

我是研究僧i 提交于 2019-12-20 13:22:50
问题 I want to loop through each item in my model in my razor view but I want to group all items together. I then want to loop through each group. Imagine I have a table: ID GroupNo GroupName 1 1 Group1 2 1 Group2 3 1 Group3 4 2 Group1 5 2 Group2 6 3 Group56 I want to do something like: @foreach (var group in Model.GroupNo) { <section> <header>Group No is @group.GroupNo</header> @foreach (var item in group) { <p>GroupName: @item.GroupName</p> } </section> } So my output is: Group No is 1 GroupName

“Remember Me” with asp.net web pages

无人久伴 提交于 2019-12-20 12:47:05
问题 I realize that this question may have been asked before, but I can't find anything that matches my situation exactly. I created a website using the WebMail helper in ASP.Net web pages ( not web forms) and WebMatrix. Users are required to login to the website, and there is a "Remember me" box that (in theory) will keep the user logged in until he/she chooses to log out. The website does keep users logged in if they close the browser and reopen it within 20-30 minutes. However, after 20-30

MVC 3 Razor, Helpers with custom markup/section

荒凉一梦 提交于 2019-12-20 12:36:52
问题 I'm not even sure if this is possible, but I thought I would check to see if there is any way to make this easier. First, I have some repeated markup in my site that looks like this: <div class="module"> <h3>Title</h3> <div> <p>Information goes here</p> </div> </div> What I want to do is wrap this up in some kind of helper/section so that I could do something like this @MyHelper("This is my Title") { <p>Here is my custom markup</p> } Then, when it renders, it would inject the title passed in

Cancel button in form

流过昼夜 提交于 2019-12-20 12:20:52
问题 I have a cancel button in a form: @using (Html.BeginForm("ConfirmBid","Auction")) { some stuff ... <input type="image" src="../../Content/css/img/btn-submit.png" class="btn-form" /> <input type="image" src="../../Content/css/img/btn-cancel.png" class="btn-form" /> } The issue is I want this button to go to a particular view when I click on it. How do I do this? 回答1: Either you can convert the Cancel button as an anchor tag with @Html.ActionLink helper method and apply a css class which makes

How do you convert a Razor view to a string?

跟風遠走 提交于 2019-12-20 12:07:26
问题 I would like to use my Razor view as some kind of template for sending emails, so I would like to "save" my template in a view, read it into controller as a string, do some necessary replacements, and then send it. I have solution that works: my template is hosted somewhere as an HTML page, but I would like to put it into my application (i.e. in my view). I don't know how to read a view as a string in my controller. 回答1: I use the following. Put it on your base controller if you have one,