html-helper

Why is my MVC ViewModel member overridden by my ActionResult parameter?

百般思念 提交于 2021-02-07 07:17:38
问题 Is this a bug or a feature? All code below has been simplified for the sake of brevity and easy replication and does not actually do anything useful other than highlight the behavior. I have a class that includes an int named ID: public class FooterLink { public int ID { get; set; } } In my controller, I have an Edit actionresult that takes a parameter called 'id': public ActionResult Edit(int id) { return View(new FooterLink() { ID = 5 }); //notice that I am explicitly setting the ID value

How to do not repeat a razor code in an ASP.NET MVC “edit” view for each model property?

孤街浪徒 提交于 2021-01-28 10:11:36
问题 If you use ASP.NET MVC, then the following code must be familiar to you: <div class="row"> <div class="form-sm-4"> @Html.LabelFor(m => m.att) </div> <div class="form-sm-8"> @Html.EditorFor(m => m.att, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(m => m.att) </div> </div> This is a basic input group set with label, input and validation message. Today I'm facing a POCO class with dozens of attributes. I mean It has N number of properties in model class.

How to do not repeat a razor code in an ASP.NET MVC “edit” view for each model property?

删除回忆录丶 提交于 2021-01-28 10:01:57
问题 If you use ASP.NET MVC, then the following code must be familiar to you: <div class="row"> <div class="form-sm-4"> @Html.LabelFor(m => m.att) </div> <div class="form-sm-8"> @Html.EditorFor(m => m.att, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(m => m.att) </div> </div> This is a basic input group set with label, input and validation message. Today I'm facing a POCO class with dozens of attributes. I mean It has N number of properties in model class.

What happened to HtmlHelper's 'ViewContext.Controller'?

依然范特西╮ 提交于 2021-01-27 19:20:32
问题 In my old ASP.NET web app, I coded an HTML helper to get access to the context of the controller executing the current view by accessing ViewContext.Controller : public static string GetControllerString(this HtmlHelper htmlHelper) { string controllerString = htmlHelper.ViewContext.Controller.ToString(); return ".NET Controller: " + controllerString; } However, this no longer seems to exist in ASP.NET Core's HTML helper object: public static string GetControllerString(this IHtmlHelper

What ../path and ~/path Indicates in MVC?

六月ゝ 毕业季﹏ 提交于 2020-06-12 06:29:08
问题 I have one custom helper class called Image which is having two arguments: src alt Now I am going to call that in one of View, @Html.Image("../Images/Indian.gif","Image is not supported or exist") <img src="~/Images/Indian.gif" alt="Image is not supported or exist" /> Now, this both will give me same result but I am confused that why the path was not the same for both and what "../path" and "~/path" indicate? This two-line it generates when I do inspect element in a web browser: <img alt=

MVC5 Lost value in Textbox after Submit

与世无争的帅哥 提交于 2020-02-24 11:08:28
问题 I would like to clarify one thing about MVC5 Razor Views. I was told that I don't have to use HTML Helpers (@Html.EditorFor, @Html.TextBoxFor) in my razor views and it will still work. Controller Code [HttpPost] public ActionResult Create(Models.TestObj obj) { ViewBag.TestStr = string.Format("You typed: {0} {1}", obj.FirstName, obj.LastName); return View(obj); } Razor View @using (Html.BeginForm()) { <div> @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form

MVC Html Helper: modelmetadata from string expression

╄→尐↘猪︶ㄣ 提交于 2020-02-04 02:49:08
问题 I am trying to build an html helper that would have access to modelmetadata. I need both versions of helper to work: from string expression and from lambda expression: Example: public static MvcHtmlString MyLabel(this HtmlHelper html, string htmlFieldName) { return LabelHelper(html, htmlFieldName); } public static MvcHtmlString MyLabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression) { return LabelHelper(html, ExpressionHelper.GetExpressionText

MVC Html.LabelFor tooltip

那年仲夏 提交于 2020-01-30 12:11:18
问题 Trying find out how to add a bit more space between the icon and the text <td>@Html.LabelFor(m => m.MiscellaneousSettings.StudentPhotoUrl, new { @class = "fa fa-question-circle", Title = "SomeTooltips" }) </td> As you can see its really close: 回答1: Add this in your css: label.fa:before { margin-right: 5px; } Which will result in: See this JSFiddle. 回答2: Add a space before the student photo URL <td>@Html.LabelFor(m => " " + m.MiscellaneousSettings.StudentPhotoUrl, new { @class = "fa fa