ASP.NET MVC: Execute Razor from Db String

ASP.net MVC: Execute Razor from DB String?

Update (I forgot all about this)

I had asked this question previously (which lead me to create RazorEngine) Pulling a View from a database rather than a file

I know of at least two: RazorEngine, MvcMailer

I have a bias towards RazorEngine as it's one that I've worked on but I have a much simpler one at Github called RazorSharp (though it only supports c#)

These are all pretty easy to use.

RazorEngine:

string result = RazorEngine.Razor.Parse(razorTemplate, new { Name = "World" });

MvcMailer

I haven't used this one so I can't help.

RazorSharp

RazorSharp also supports master pages.

string result = RazorSharp.Razor.Parse(new { Name = "World" }, 
razorTemplate,
masterTemplate); //master template not required

Neither RazorSharp, nor RazorEngine support any of the Mvc helpers such as Html and Url. Since these libraries are supposed to exist outside of Mvc and thus require more work to get them to work with those helpers. I can't say anything about MvcMailer but I suspect the situation is the same.

Hope these help.

Execute Controller Method from razor html view in MVC?

If your Action method's parameter name is id,

public ActionResult output(string id)
{
//do something
}

then you may use your form action url like this.(The default routing will take care of rest)

/Shops/output/somestringhere.

If you have a different name, use that as the query string

public ActionResult output(string name)
{
//do something
}

Now use your form action url like

/Shops/output?name=somestringhere

Another suggestion about your code is to avoid Viewdata for rendering the dropdown. Try to use strongly typed view model and it's properties for transfering data to your view. Also try to move your javascript from your view and make it unobutrusive. So that your view stays as clean markup only.

Assuming you want to show a Revision dropdown in a document create view, Add a property to your viewmodel to have the dropdown items.

public class DocumentCreateViewModel
{
//Other properties also here

public List<SelectListItem> Revisions{ set;get;}
public int SelectedRevision { set;get;}

public DocumentCreateViewModel()
{
Revisions=new List<SelectListItem>();
}
}

and in your GET action, fill the dropdown content to the Revisions property.

public ActionResult Create()
{
var vm=new DocumentCreateViewModel();
vm.Revisions=GetRevisionItemsFromSomeWhere();

return View(vm);
}

And in your strongly typed view,

@model DocumentCreateViewModel

@using(Html.Beginform())
{

@Html.DropDownListFor(x => x.SelectedRevision,
new SelectList(Model.Revisions,"Value","Text"), "Select..")

<input type="submit" />
}

Now to handle the form submit on change event of dropdown, add this script.

$(function(){

$("#SelectedRevision").change(function(){
var _this=$(this);
var selectedRevision=_this.val();
$("form#YourFormIDHere")
.attr("action","./Shops/output/"+selectedRevision).submit();
});

});

Instead of hardcoding the url to shops/output, you may use the razor helper method(@Url.Action) to get the proper path.

Concatenating strings in Razor

Use the parentesis syntax of Razor:

@(Model.address + " " + Model.city)

or

@(String.Format("{0} {1}", Model.address, Model.city))

Update: With C# 6 you can also use the $-Notation (officially interpolated strings):

@($"{Model.address} {Model.city}")


Related Topics



Leave a reply



Submit