Using Visual Studio 2012 / MVC 4
When you create a controller in VS, you get the option to have VS automatically generate CRUD templates for your controller, is it possible to modify these templates or even create your own templates? As every time these templates are created I need to make minor changes to the code it generates, example
this is the code VS generates for my edit template
<div class="editor-label">
#Html.LabelFor(model => model.Title)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Title)
#Html.ValidationMessageFor(model => model.Title)
</div>
but I have to change these evey time to suit my layout, to something like
<section>
#Html.LabelFor(model => model.Title)
<div>
#Html.EditorFor(model => model.Title)
#Html.ValidationMessageFor(model => model.Title)
</div>
</Section>
See the comment from Omar for the answer
Related
I have two ASP.NET MVC EF entities sharing an association:
When I am editing or creating the User, I'd like to be able to attach one or more UserRoles. So, my User Create view looks like this:
#model MyModels.UserViewModel
#{ ViewBag.Title = "Create"; }
#using (Html.BeginForm())
{
<fieldset>
<legend>User</legend>
<div class="editor-label">
#Html.LabelFor(model => model.Username)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Username)
#Html.ValidationMessageFor(model => model.Username)
</div>
<!-- how to create an editor to add/remove roles -->
<div class="editor-label">
#Html.LabelFor(model => model.UserRoles)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.UserRoles)
#Html.ValidationMessageFor(model => model.UserRoles)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
Since Model.UserRoles is initially empty, I get nothing in this line #Html.EditorFor(model => model.UserRoles). I tried adding a drop-down where I can select existing roles using JavaScript (I guess retrieved as partial view?), but I don't know how to append a selected role to the fieldset so that it gets submitted.
How is this usually done in ASP.NET? I have been searching SO for a while but cannot find the appropriate solution.
To clarify, what I don't understand is how to add/append a new role to the client side, so that the submit button actually sends this inside the UserRoles list.
(edit) Ok, I've found this: How to add an item to a list in a ViewModel using Razor and .NET Core. It says that I should fetch a partial view which is written to look like the inputs which would be generated by #Html.EditorFor (with the next array index), and then append it using JavaScript.
That looks quite weird, is that really the correct approach?
Can I insert html designing/code with this Razor CSHTML code in same page?
#model project_final.Models.Bike
#{
ViewBag.Title = "Create";
}
<h2>Create</h2>
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
#<legend>Bike</legend>
#<div class="editor-label">
#Html.LabelFor(model => model.Id)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Id)
#Html.ValidationMessageFor(model => model.Id)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Color)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Color)
#Html.ValidationMessageFor(model => model.Color)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
Yes, you can.
Just beware of what is the code that need to have # in Razor engine, and what is html tag don't need the # like what #Stephen Muecke mentioned.
#Bike does not need the # in front of it because is html tag. Or I make a simple statement that
# never stick with <, if you found #< in your ASP.Net View, it must be a
mistake
Read the Introduction to ASP.NET Web Programming Using the Razor Syntax (C#), may be you can know more things about Razor.
By the way , you might need to read the post How can I add a class attribute to an HTML element generated by MVC's HTML Helpers? to modified the class of the HTML helper such as #Html.LabelFor
#Html.LabelFor(model => model.Id, new { #class = "col-md-2 control-label" })
Masking is working fine but I want to Auto generate year section by default like : 123-1234/(currentyear).
View.cshtml
<fieldset class="CreateEditfieldset Seconfieldset">
<div class="editor-label">
#Html.LabelFor(model => model.DONO)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.DONO)
#Html.ValidationMessageFor(model => model.DONO) // format : 123-1234/2014
</div>
</fieldset>
<script type="text/javascript">
$("#DONO").mask("999-9999/9999");
</script>
It's editorfor Id is "DONO" and masking is working fine I repeat.
One possible (hack-ish) way is to do this on the server:
$("#DONO").mask("999-9999/#DateTime.Today.Year");
Im trying to use Foundation grid in my application but I cannot simply center the fieldset. I try to put in the center of the screen on mobile device and desktops, but it just looks weird. Any idea why?
Please, note that Im new to Foundation and I have no experience with Bootstrap
Code below:
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
<div class="row">
<div class="large-6 large-centered columns">
<fieldset>
<legend>Logging into Review Platform</legend>
<div class="editor-label">
#Html.LabelFor(model => model.UserName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.UserName)
#Html.ValidationMessageFor(model => model.UserName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Password)
#Html.ValidationMessageFor(model => model.Password)
</div>
<p>
<input type="submit" value="Login" class="button" />
</p>
</fieldset>
</div>
</div>
}
A repeat and edit of my comment on the original question:
Are you wanting full width on mobile and centered on desktop? I do this quite often with something like:
.small-12.large-6.large-centered.columns
or you can do something like
.small-10.small-centered.large-6.columns
You don't need the large-centered because it inherits it from the small-centered class. If you want the larger grid to not be centered, use .large-uncentered
I have a lot of repeated code in my asp.net MVC views
<div class="editor-label">
#Html.LabelFor(model => model.MyProperty)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.MyProperty)
#Html.ValidationMessageFor(model => model.MyProperty)
</div>
I want to wrap this in something like:
foreach (System.Reflection.PropertyInfo item in Model.GetType().GetProperties())
and reuse the same partial view throughout. But I can't get the syntax quite right.
How can I make this more generic?
This is what Display and EditorTemplates are for.
You need only do #Html.EditorForModel() and then define a generic template for your model.