Html.EditorFor Global Template? - c#

Is there any way to define a global template for the Html.EditorFor helper?
I would like to alter the markup that is output so that for example instead of rendering
<div class="editor-label">
<label .../>
</div>
<div class="editor-field">
<input .../>
</div>
It would render:
<div>
<div class="label"><label..../></div>
<div class="field"><input..../></div>
</div>
This is for when I'm using Html.EditorFor with an object instance not just an object property.

Brad Wilson has written a blog series on asp.net mvc 2 templates. This one is related to your problem.

Related

ASP.NET - Read Component Parameter In Razor View

I have an existing ASP.NET app that uses one Razor component throughout. Unfortunately, this component does not have a model associated with it. I'm in a scenario where I need to add one parameter. At this time, I have the following in the component host view:
#await Component.InvokeAsync("MyTextField", new { Align = "Left" })
The component Razor code currently looks like this:
MyTextField.cshtml
<div class="text-right">
<form asp-action="ReadItem" asp-controller="Inventory" method="get" id="inventory-form">
<input id="inputField" class="input-text" type="text" />
<button class="submit-button" type="submit">Submit</button>
</form>
</div>
Currently, the component renders. However, I want to get the value of the Align parameter, if it exists, in the MyTextField.cshtml view. Is there a way for me to get a parameter value there? If so, how?
Thanks
You can do that in this way:
#await Component.InvokeAsync("MyTextField", new User{ Align = "Left" })
and in View (cshtml file):
#using UserNamespace
#model User
<div class="text-right">
<form asp-action="ReadItem" asp-controller="Inventory" method="get" id="inventory-form">
<input id="inputField" class="input-text" type="text" value="#Model.Align" />
<button class="submit-button" type="submit">Submit</button>
</form>
</div>

How to get rid of default label in EditorForModel

I have an ASP.Net MVC project that I'm working on and I have a model that I want to turn into I form. I want to use EditorForModel() for this because I want it to automatically add fields to the form if I decide to add/remove properties to the model later.
The problem I'm having is that editor for model takes each property and creates the following HTML: (this is just generic. not the actual code)
<div class="editor-label"><label>Label Title</label></div>
<div class="editor-field"><input type="type"/></div
However what I want it do do for each field is this:
<div class="editor-field">
<label>Label Title</label>
<input type="type"/>
</div>
But if I try to do this in an editor template I get this instead:
<div class="editor-label"><label>Label Title</label></div>
<div class="editor-field">
<label>Label Title</label>
<input type="type"/>
</div>
How can I get EditorForModel() to not make it's own label field? I know I could always style it as hidden in CSS but it'd be nice if it wasn't even there to begin with so I could limit code confusion.
Thanks in advance.
PS: I've also tried using [HiddenInput(DisplayValue = false)] but this just hides the entire field and not just the label.
You need to write this in your editor template cshtml to get it work as expected:
#foreach (var property in ViewData.ModelMetadata.Properties)
{
<div class="editor-field">
<label>#property.GetDisplayName()</label>
#Html.Editor(property.PropertyName)
</div>
}

Use HtmlHelper in Kendo Template

What I'm trying to do seems pretty simple but I'm not sure if it's possible or not.
I have a Kendo Template and I am wanting to use an HtmlHelper that I created inside of it but I'm having an issue with passing the value into it.
This is what my template looks like:
<script type="text/x-kendo-tmpl" id="reviewTemplate">
<div class="panel panel-default">
<div class="panel-heading">
<div class="panel-title">
#:ProducerProfileDisplay#
</div>
</div>
<div class="panel-body">
#Html.Raw(Html.DisplayRating(#:Rating#))
<p>#:ReviewText#</p>
</div>
</div>
</script>
The problem with this is it just doesn't like the syntax at all and I can't compile my project. And if I decide to put it inside of quotes then it will literally just pass in the string #:Rating#.
By the way, the method I created is expecting a double.
Any ideas?

How to check in razor view if field is valid?

I use bootstrap with ASP.NET Core and to indicate form field validation errors i want to add has-errors class to form-group div when given field has an error. The view looks like that:
<div class="form-group">
<label asp-for="Fragment.Content" class="col-lg-2 control-label "></label>
<div class="col-lg-10">
<textarea asp-for="Fragment.Content" class="form-control content-editor"></textarea>
<span class="help-block">A longer block of help text that breaks onto a new line and may extend beyond one line.</span>
<span asp-validation-for="Fragment.Content"></span>
</div>
</div>
I would like to do something like:
<div class="form-group" asp-add-class-if-error="has-errors" for-field="Fragment.Content"/>
I know i can write my own tag helper, however i am curious if there is a built-in solution.
I found that you can use:
#using Microsoft.AspNetCore.Mvc.ModelBinding
#if(ViewData.ModelState.GetFieldValidationState("Fragment.Content") == Microsoft.AspNetCore.Mvc.ModelBinding.ModelValidationState.Invalid)
{
// something here
}

MVC - Nested Partial Views

I am getting an error ("Server Error in '/' Application") when I nest partial views in my MVC app. The views work fine individually, but not when nested. It's my understanding that it's okay to do this, so what am I doing wrong?
Essentially, I am trying to use partials as sub-layouts within my _Layout.cshtml.
Here's my main layout - _Layout.cshtml
<!DOCTYPE html>
<html>
<head>...</head>
<body style="padding-top: 80px;">
<div class="container-fluid">
<div class="row">
<div id="myTab" class="col-lg-12 col-md-12 col-sm-12">
...
<div class="tab-content">
<div class="tab-pane fade" id="search">
#Html.Partial("~/Areas/Search/Views/Shared/_SearchLayout.cshtml")
</div>
</div>
</div>
</div>
</div>
#RenderBody()
#RenderSection("scripts", required: false)
</body>
</html>
This is the first partial view (_SearchLayout). If I remove the partials AND #RenderBody, no error.
<div class="container-fluid">
#Html.Partial("_PolicySearch")
#Html.Partial("_ClaimSearch")
</div>
#RenderBody()
This partial view is nested in the first partial view (_SearchLayout):
<div class="row top-buffer search-outline form-horizontal">
<div class="col-md-1 search-icon-size text-primary">
<i class="glyphicon glyphicon-heart"></i>
</div>
<div class="col-md-1 search-icon-size text-primary">
<h4>Claim Search</h4>
</div>
</div>
In your first partial view:
Remove RenderBody
Replace Html.Partial to Html.RenderPartial
I also would recommend to rename your partial view to something not containing the word "Layout" to avoid the mismatch between the view types.
Use Html.RenderPartial instead
The problem is #RenderBody(). This can only be called in a layout, which when used in this way _SearchLayout.cshtml is not, despite its name.
The important thing to remember about layouts, partials and views in ASP.NET MVC is that they're all views. The only thing that differentiates them is how they're used. In this instance, you're using the _SearchLayout.cshtml view as a partial, and partials can't use #RenderBody().

Categories

Resources