Any good replacment for Razor Engine - c#

As you know Razor Engine doesn't work under MVC4.
So I am wondering if we can use Model object to generate email html body in some other way.
Any clue which tool/dll we can use for it.
I really don't want to put attribute names manually and use Replace method if I have 100 fields in the Model...
Sample:
<div class="display-label">
#Html.DisplayNameFor(model => model.Name)
</div>
<div class="display-field">
#Html.DisplayFor(model => model.Name)
</div>
<div class="clear">
</div>

Razor Engine now has Razor 2 compatibility (introduced in 3.1)

I've been using ActionMailer.net to generate html emails using Razor views. You can find it on NuGet or here: https://bitbucket.org/swaj/actionmailer.net/wiki/Home

Related

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>
}

How does MVC framework assign model to a view while returning it from Controller?

This is a bit confusing. Till today i knew the following ways to pass data to view in MVC
ViewBag,ViewData,TempData,Strongly Typed Views and its Models.
So wherever we used a strongly typed view, we used to pass a model with data or empty object to a view, so that it does not throw any null reference error.
But today encountered a behavior that made me feel strange.
Case-1
The following is the EmployeeController's Create action
//
// GET: /Employee/Create
public ActionResult Create()
{
return View("Create");
}
The following is the CreateView inside Employee Folder or Views.
#model EmployeeDataBase.Models.Employee
<fieldset>
<legend>Employee</legend>
<div class="editor-label">
#Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Name)
#Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Email)
#Html.ValidationMessageFor(model => model.Email)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
I am not returning any model in the action,but still the view was rendered.
Case-2
My action
public ActionResult Create(Employee employee, Employee emp)
{
return View("Create");
}
Called the above using the following URL
http://localhost:50128/Employee/Create?Name=something
Both the Employee parameters in the action were instantiated with Name property value as "something". Did not return anything in the action, still create is rendered. If if a dynamically change the value of Name during debugging, its still shows "something" in Create view in the Name text box.
Case 1:
If you had passed a model into the view, it would have displayed that model. If you do not pass a model, the view simply uses your model class to create the controls. In this case it is creating labels, textboxes and validation for all the properties. It uses those lambda expressions in LabelFor (xxxFor) as Expression Trees and analyses your class. It analyses and looks for things like: In the model you may have used a Display("Full Name") attribute for Name property, therefore it will conclude you want to show "Full Name" in label instead of "Name". It creates validation javascript in the same manner.
Therefore, in order to use the expression tree given in lambdas, it does not need an instance of the model.
Case 2:
Has been addressed in this SO question.

How to recognize a quote symbol (")

Enviroment: Visual Studio 2012, MVC4, Razor, Internet Application.
I have a code with search form in the "View" page...
#using (Html.BeginForm("SearchResult", "Home", FormMethod.Get))
{
#Html.ValidationSummary(false)
<fieldset>
<legend>Contact Search</legend>
<div class="editor-label">
#Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Name)
#Html.ValidationMessageFor(model => model.Name)
</div>
<p>
<input name="SearchButton" type="submit" value="Search" /></p>
</fieldset>
}
And then I'm sending a "model.Name" string directly to the JavaScript code using "#Model.Name".
The problem is... when I typing in the search form a quote (") symbol... for example... (10" android) I have a problem. JavaScript stops work somewhere. How can I check this "model.Name" string inside the controller is it contains (") or not and change it for JavaScript?
My JavaScript code... url += "&keywords=#Model.Name";
If "model.Name" is... 10" android ...will it work correct?
For Javascript, I would encode with "escape", but it is best practice to replace the " (double) quotes with the XML representation "
As for server-side, I use Web Protection Library from Microsoft to handle that part,
http://wpl.codeplex.com/
Microsoft.Security.Application.Encoder.HtmlEncode(HttpUtility.HtmlDecode(dr["message"].ToString()))

Changing the size of Html.TextBox

I'm developing an ASP.NET MVC3 application using the new Razor view engine but I'm having some difficulty changing a TextBox so that it is multiline.
So far all I've been able to find via google is that I need to set the multiline property to true, but I'm not sure how.
View code looks like this.
<div class="editor-field">
#Html.TextBoxFor(model => model.Body)
</div>
Any suggestions?
You could decorate the Body property on your view model with the [DataType] attribute:
[DataType(DataType.MultilineText)]
public string Body { get; set; }
and in your view use the EditorFor helper instead of TextBoxFor:
<div class="editor-field">
#Html.EditorFor(model => model.Body)
</div>
Another possibility is to leave the model as is without adding any attributes to it and in your view use the TextAreaFor helper:
<div class="editor-field">
#Html.TextAreaFor(x => x.Body)
</div>
Personally I prefer the first approach.
This is how to curve the corners of the textbox in asp.net mvc4:
#HTML.TextBoxFor(Model => Model.Name)

Html.EditorFor Global Template?

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.

Categories

Resources