How to integrate MVC4 with SmartAdmin? - asp.net-mvc

I am using SmartAdmin for UI purpose and MVC4 for coding. SmartAdmin CSS is not supported inside
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<div class="row">
<section class="label col col-1">
#Html.LabelFor(model => model.Name, new { #style = "text-align:left" })
< /section>
<section class="col col-4">
<label class="input"> <i class="icon-append fa fa-user"></i>
#Html.TextBoxFor(model => model.Name, null, new { #placeholder = "Enter Name" })
#Html.ValidationMessageFor(model => model.Name)
</label>
</section>
<section class="label col col-1">
#Html.LabelFor(model => model.Role)
</section>
<section class="col col-4">
<label class="input"> <i class="icon-append fa fa-user"></i>
#Html.TextBoxFor(model => model.Role, null, new { #placeholder = "Enter Role" })
#Html.ValidationMessageFor(model => model.Role)
</label>
</section>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
But by using code above, UI does not looks in proper format. When I remove
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
from code then UI looks properly but does not posts values to controller.Now my issue is that, can MVC4 supports SmartAdmin for UI purpose?
Is there any solution for that?

Here is a great tutorial for that:
http://myorange.ca/supportforum/question/how-to-integrate-smartadmin-1-4-x-into-asp-net-mvc-5

add the "smart-form" tag into the class field like below to activate it.
Html.BeginForm("ACTIONNAME", "CONTROLLERNAME", FormMethod.Post, new { #class = "smart-form ", id = "myID" }))
But if you are starting with smartadmin and MVC, i highly recommand you to follow the tutorial:
http://myorange.ca/supportforum/question/how-to-integrate-smartadmin-1-4-x-into-asp-net-mvc-5

I use a bootstrap based template. It works very well for me, in my code I do not use the form helper, so more along the lines of . Try doing this instead to see if that helps. It is entirely possible that the helper is allocating a class to the form which throws out the CSS formatting. Using something like Firebug to look at the effective CSS will help you.

Related

ASP.NET RAZOR - How to Bind razor checkBox on custom Boostrap 4 Checkbox Switch

How would I put the codes generated by the razor on this switch?
It would work like the ckeckbox
Image Discription
An here is the link how its work:
https://bootsnipp.com/snippets/GaxR2
<div class="card" style="margin:50px 0">
<div class="card-header">Checkbox to Switch</div>
<ul class="list-group list-group-flush">
<li class="list-group-item">
Bootstrap Switch Success
<label class="switch ">
<input type="checkbox" class="success">
<span class="slider"></span>
</label>
</li>
</ul>
</div>
Razor Code
<div class="form-group">
#Html.LabelFor(model => model.ExibirInfoPosicionamento, htmlAttributes: new { #class = "control-label " })
<div class="col-md-10">
#Html.CheckBoxFor(model => model.ExibirInfoPosicionamento, new { })
</div>
</div>
Little late but I had some frustration with this too, got it working like this (Bootstrap 4.3.1)
Razor on my page --
<div class="col-lg-1">
#Html.EditorFor(m => Model.YN,"toggle", new { #id = "yn1 })
</div>
Editor Template 'toggle'
#model System.Boolean
<div class="custom-control custom-switch">
#Html.CheckBoxFor(m => m,
new { #class = "custom-control-input" })
#Html.LabelFor(m => m, new { #class = "custom-control-label" })
</div>

How to redirect button that is inside Html.BeginForm

I have this View which displays 3 text boxes and a drop down list for displaying nationalities. The form itself works just fine, posts back to the controller and nothing is wrong. However what i want is, say a nationality is not present in the drop down and a user clicks a button which would redirect to a different page where they can add a nationality. Here is what i have so far:
#model WebApp.Models.ViewModels.AddPersonViewModel
<div class="container card">
#using (Html.BeginForm("AddPersonPost", "Persons", FormMethod.Post))
{
<form>
<div class="form-row">
<div class="col">
#Html.TextBoxFor(m => m.PersonModel.Name, new { #class = "form-control", #placeholder = "Name" })
</div>
<div class="col">
#Html.TextBoxFor(m => m.PersonModel.Relationship, new { #class = "form-control", #placeholder = "Relationship" })
</div>
<div class="col">
#Html.TextBoxFor(m => m.PersonModel.Comment, new { #class = "form-control", #placeholder = "Comment" })
</div>
<div class="col dropdown show">
#Html.DropDownListFor(m => m.PersonModel.Nationality, Model.NationalityList)
<input type="submit" class="btn-sm btn-primary mt-1" value="Add Nationality" onclick="window.location='#Url.Action("GetAll", "Persons")'">
</div>
</div>
<div class="m-sm-2">
<input type="submit" class="btn-sm btn-primary" value="Add Person">
</div>
</form>
}
</div>
I have tried the redirect option but when i click the button to add nationality it keeps going to the Action in the BeginForm, not to the intended Action in the button. So i was wondering 1. is this good practice to do what i am attempting to do? 2. How to go about it? Any help is appreciated.

MVC Model Property Binding in pop-up template editor

I am using Telerik MVC Grid with a pop-up editor. I have an editor template defined with the following code, and it is working. However, it seems like the model binding only works with certain methods that take a lambda expression (#Html.EditorFor, #Html.TextBoxFor, etc). If I simply wanted to make a model property appear as raw html/text in the page - how is that done? I have tried using the #Model.Property syntax - and it does not produce an error, but no value is output. What am I overlooking here?
#model Models.MatrixConditionViewModel
<div style="padding:5px;margin:5px;width:975px;border:1px solid black;" class="form-horizontal m-t-md">
<h3>Edit Condition</h3>
<br />
<div class="form-group">
#Html.HiddenFor(model => model.ConditionId)
<label class="col-sm-2 control-label">Condition</label>
#Html.TextBoxFor(model => model.ConditionName, new { #class = "col-sm-3" })
#Html.ValidationMessageFor(model => model.ConditionName)
<label class="col-sm-2 control-label">Desc</label>
#Html.TextAreaFor(model => model.ConditionDescription, 3, 25, null)
#Html.ValidationMessageFor(model => model.ConditionDescription)
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Logic</label>
#Html.TextAreaFor(model => model.ConditionLogic, 5, 94, null)
#Html.ValidationMessageFor(model => model.ConditionLogic)
</div>
<div class="form-group">
Need a value here: #Model.ConditionId
<label class="col-sm-2 control-label">Content</label>
<iframe src="~/WebForms/ContentEditor.aspx?ConditionId=1" width="700" height="425" frameborder="0"></iframe>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">Font Formatting</label>
#Html.TextAreaFor(model => model.ConditionFormatNotes, 5, 94, null)
#Html.ValidationMessageFor(model => model.ConditionFormatNotes)
</div>
</div>
Use the Html.Raw HTML helper to render the property
#Html.Raw(Model.PropertyName)
If you want this to then be bound by the modebinder, you have to make sure the input name matches the params (and types of params) of the action method that is receiving the request.

ReCaptcha MVC razor Validation Message not getting triggered

I am fairly new to MVC and a little stumped on this one.
I implemented ReCaptcha last night in my Umbraco MVC 4 application (6.1.6).
ReCaptcha works, the controller gets the correct captcha valid state and all is good. The problem is when the captcha is incorrect, I set the ModelState.AddModelError(string, string) and return the model in order to show that the captcha entered as incorrect, but the view never triggers the Html.ValidateMessage("captcha-error") to show the actual error message updated in the ModelState.
Any help or advice would be appreciated
Controller:
[HttpPost]
[RecaptchaControlMvc.CaptchaValidator]
public ActionResult SubmitContactFormAction(Models.ContactFormModel model, bool captchaValid, string captchaErrorMessage)
{
if (!captchaValid)
{
ModelState.AddModelError("captcha-error", captchaErrorMessage);
return View(model);
}
else
{
// Test ModelState and do stuff
}
}
View:
#model WebStaging.Models.ContactFormModel
#using Recaptcha
#using (Ajax.BeginForm("SubmitContactFormAction", "ContactForm", null, new AjaxOptions { HttpMethod = "POST" }, new { #id = "frmContactForm" }))
{
#Html.ValidationSummary(true)
<div>
Fields marked with * are required<br/><br />
<div class="clear pad-5">
<div class="label-column">
#Html.LabelFor(model => model.Name) *
</div>
<div class="input-column">
#Html.TextBoxFor(model => model.Name, new { #class = "input-242" })<br />
#Html.ValidationMessageFor(model => model.Name)
</div>
</div>
<div class="clear pad-5">
<div class="label-column">
#Html.LabelFor(model => model.Email) *
</div>
<div class="input-column">
#Html.TextBoxFor(model => model.Email, new { #class = "input-242" })
</div>
</div>
<div class="clear pad-5">
<div class="label-column">
#Html.LabelFor(model => model.Phone)
</div>
<div class="input-column">
#Html.TextBoxFor(model => model.Phone, new { #class = "input-242" })
</div>
</div>
<div class="clear pad-5">
<div class="label-column">
#Html.LabelFor(model => model.Subject) *
</div>
<div class="input-column">
#Html.TextBoxFor(model => model.Subject, new { #class = "input-242" })
</div>
</div>
<div class="clear pad-5">
<div class="label-column">
#Html.LabelFor(model => model.Message) *
</div>
<div class="input-column">
#Html.TextAreaFor(model => model.Message, new { #class = "input-textarea" })
</div>
</div>
<br />
<div id="captcha-panel" class="clear" style="text-align: right; float: right; padding-top: 5px;">
#Html.Raw(Html.GenerateCaptcha("captcha", "white"))
</div>
<p>
<div id="captcha-result" class="clear">
#Html.ValidationMessage("captcha-error")
</div>
<div class="clear pad-5" style="float: right; text-align: center;">
<button type="submit">Send</button><br />
<div id="submit-status" style="color: Green;"></div>
</div>
</div>
I think problem is caused because ReCaptcha is not a simple web control. I think you can easy walk around problem changing your "captcha-result" div with new class or style attribute.
<div id="captcha-result" class="someClassWithRedBigFont" >
#Html.ValidationMessage("captcha-error")
</div>
If you using jQuery you can check jQuery Validate plugin.

MVC4 - How to rearrange generated fields created by Scaffolding

I have a .cshtml file created by MVC4 scaffolding that looks something like this:
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<legend>BicycleSellerListing</legend>
<div class="editor-label">
#Html.LabelFor(model => model.BicycleManfacturer)
</div>
<div class="editor-field">
#Html.DropDownList("ManufacturerList")
</div>
<div class="editor-label">
#Html.LabelFor(model => model.BicycleType)
</div>
<div class="editor-field">
#Html.DropDownList("TypeList")
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ListingPrice)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ListingPrice)
#Html.ValidationMessageFor(model => model.ListingPrice)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Comments)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Comments)
#Html.ValidationMessageFor(model => model.Comments)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
Instead of the labels and columns being generated in a single vertical column, I would like to rearrange this so that I have labels in one column and editor fields in a second column (more of a traditional data entry form). I am very new to MVC 4 and HTML5 and don't really know how to go about doing this. If someone could help me rearrange this .cshtml code to accomplish this, I would be very thankful.
Here is one way using float:left and :before
http://jsfiddle.net/fdLca/
.editor-label {
float:left;
width:20%;
}
.editor-label:before {
clear:left;
}
.editor-field {
float:left;
width:80%;
}
To get around using :before if you needed to support older IE (< 9) then you can add an element purely being used to clear in between each field and label.
http://jsfiddle.net/fdLca/1/
HTML
<div class="editor-label">
label1
</div>
<div class="editor-field">
#Html.DropDownList("ManufacturerList")
</div>
<div class="clear"></div>
CSS
.clear {
clear:left;
}

Resources