Replace EditFor to a chackbox - asp.net-mvc

New to MVC, I am using a vs2019 template linked to CRM using CData. I have created an edit view. The new_fm-bankdata is a boolean field. The EditFor display a dropdown box with True / False / Not Set, with the current value displayed. How can I change this to a check box
`#ModelType xxxx.Contact
#Code
ViewData("Title") = "Edit"
End Code
#Using (Html.BeginForm())
#Html.AntiForgeryToken()
#<div class="form-horizontal">
<h4>Edit User : #Session("FullName")</h4>
<hr />
#Html.ValidationSummary(True, "", New With {.class = "text-danger"})
#Html.HiddenFor(Function(model) model.Id)
<div class="col-md-10">
#Html.EditorFor(Function(model) model.new_fm_bankdata, New With {.htmlAttributes = New With {.class = "form-control"}})
#Html.ValidationMessageFor(Function(model) model.new_fm_bankdata, "Required", New With {.class = "text-danger"})
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
End Using
<div>
#Html.ActionLink("Back to List", "Index")
</div>
#Section Scripts
#Scripts.Render("~/bundles/jqueryval")
End Section`

just try to read more about Razor
#Html.CheckBoxFor(m=> m.new_fm_bankdata, new {#class = "form-control"}})

Related

Radio-Buttons overlapping each other

I am building a form using bootstrap 4.3 with few Radio-Buttons, but the issue is, it overlaps each other as shown below.
I tried it in jsfiddle and the result is same. Here is my working in jsfiddle: https://jsfiddle.net/hifni/ky3xzej0/7/
My Razor page looks like this:
<div class="alert alert-info">
<p>The request will enable your Credit Balance to be in your account for future trading/settlement.</p>
<footer class="blockquote-footer">Subject to T&C</footer>
</div>
<style>
.form-control {
height: auto;
}
</style>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form">
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.HiddenFor(model => model.AccountId)
<input type="hidden" id="tempStore" />
#Html.HiddenFor(model => model.SendSMS)
#Html.HiddenFor(model => model.RequiresApproval)
</div>
<div class="form-group">
<label>Pick a Settlement Mode</label>
<div class="form-control">
<div class="form-check">
#Html.RadioButtonFor(model => model.IsSettlementOnHold, "No", new { #class = "form-check-input", #id = "rbIsSettlementOnHold_No" })
#Html.Label("rbIsSettlementOnHold_No", "Standard ", new { #class = "form-check-label" })
#Html.RadioButtonFor(model => model.IsSettlementOnHold, "Yes", new { #class = "form-check-input", #id = "rbIsSettlementOnHold_Yes" })
#Html.Label("rbIsSettlementOnHold_Yes", "On Request ", new { #class = "form-check-label" })
#Html.ValidationMessageFor(model => model.IsSettlementOnHold, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
#Html.TextAreaFor(model => model.AdditionalNote, 3, 50, new { #class = "form-control", #placeholder = "Any Aditional Note" })
#Html.ValidationMessageFor(model => model.AdditionalNote, "", new { #class = "text-danger" })
</div>
<div class="form-group">
#*<input type="submit" value="Submit" class="btn btn-default" id="btnSelfServiceRequestSubmit"/>*#
<button type="submit"
class="btn btn-dark"
id="btnSelfServiceRequestSubmit"
#*data-toggle="modal"
data-target="#modalEntityDetail"
data-url="#Url.Action("_CodePrompt", "ExternalSelfServiceRequest")"*#>
Submit
</button>
<button type="reset"
class="btn btn-warning">
Reset
</button>
</div>
#if (Model.RequiresApproval.Equals("Yes"))
{
<div class="alert alert-info" role="alert">
For security verification purpose you will be required to access your JKSB Registered Mobile, there will
be a verification code sent to it.
</div>
}
</div>
}
Appreciate any help.
You can separate your "form-check" div like this;
<div class="form-control">
<div class="form-check form-check-inline">
<input checked="checked" class="form-check-input" id="rbIsSettlementOnHold_No" name="IsSettlementOnHold" type="radio" value="No">
<label for="rbIsSettlementOnHold_No" class="form-check-label"> Standard</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" id="rbIsSettlementOnHold_Yes" name="IsSettlementOnHold" type="radio" value="Yes">
<label for="rbIsSettlementOnHold_Yes" class="form-check-label"> On Request </label>
</div>
</div>
And using "form-check-inline" class will be provide you a horizontal alignment.
I hope this helps.

Client-side Required error message not showing

I want to implement client side validation on my form, but none of the required error message is showing when I not putting data.
Scripts are loaded correctly in the layout file.
And in the rest of the views, Required messages are displayed correctly and the program works correctly .
I've sent the codes for this subject to the attachment.
controller:
<HttpPost()>
Function GalleryFiles1(ByVal docs As MachininfoDocs, ByVal imgUp As HttpPostedFileBase) As ActionResult
If ModelState.IsValid Then
If imgUp IsNot Nothing And IsImage(imgUp) Then
docs.FileName = Guid.NewGuid().ToString() & Path.GetExtension(imgUp.FileName)
docs.DocGroup = 1
docs.MachininfoID = PVMachinInfoId
imgUp.SaveAs(Server.MapPath("/Images/MachinImages/" & docs.FileName))
Dim img As New ImageResizer()
img.Resize(Server.MapPath("/Images/MachinImages/" & docs.FileName), Server.MapPath("/Images/MachinImages/Thumb/" & docs.FileName))
db.MachininfoDocs.Add(docs)
db.SaveChanges()
End If
End If
Return RedirectToAction("GalleryFiles1", New With {Key .id = docs.MachininfoID})
End Function
View:
#ModelType Machinary.MachininfoDocs
#Code
ViewData("Title") = "اسناد مالکیت"
Layout = "~/Views/Shared/_Layout.vbhtml"
End Code
#Using (Html.BeginForm("GalleryFiles1", "Machininfo", FormMethod.Post, New With {.enctype = "multipart/form-data"}))
#Html.AntiForgeryToken()
#Html.HiddenFor(Function(model) model.MachininfoID)
#<div Class="panel">
<header Class="panel-heading tab-bg-dark-navy-blue">
<Label Class="bg-transparent wht-color"> اطلاعات جامع ماشین آلات- اسناد مالکیت</Label>
</header>
<br />
<div Class="form-horizontal panel-body">
<div class="col-md-4">
#Html.ValidationSummary(True, "", New With {.class = "text-danger"})
<div class="form-group">
#Html.LabelFor(Function(model) model.Title, htmlAttributes:=New With {.class = "control-label col-md-2"})
<div class="col-md-10">
#Html.EditorFor(Function(model) model.Title, New With {.htmlAttributes = New With {.class = "form-control"}})
#Html.ValidationMessageFor(Function(model) model.Title, "", New With {.class = "text-danger"})
</div>
</div>
<div class="form-group">
#Html.LabelFor(Function(model) model.FileName, htmlAttributes:=New With {.class = "control-label col-md-2"})
<div class="col-md-10">
<input type="file" name="imgUp" class="form-control-file" />
#Html.ValidationMessageFor(Function(model) model.FileName, "", New With {.class = "text-danger"})
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="ثبت" class="btn btn-success" />
</div>
</div>
<br />
</div>
<div class="col-md-8">
#code
Dim al As List(Of MachininfoDocs) = ViewBag.Docs
End code
<Table Class="table table-bordered table-responsive-sm">
<tr>
<th class="col-md-3"> تصویر</th>
<th class="col-md-7"> عنوان</th>
<th>
</th>
</tr>
#For Each item In al
#<tr id="tbrow_#(item.Id)">
<td>
<img style="width:100% ; height:120px" src="~/Images/MachinImages/Thumb/#item.FileName" class=" img-thumbnail" />
</td>
<td>
#item.Title
</td>
<td>
<a class="btn btn-sm btn-danger" onclick="DeleteTbRow(#item.Id)">
<i class="glyphicon glyphicon-trash btn btn-danger btn-sm"></i>
</a>
</td>
</tr>
Next
</Table>
</div>
</div>
</div>
End Using
<div class="pull-left btn btn-default">
#Html.ActionLink("برگشت به لیست", "Index")
</div>
#section MyScripts
<script>
function DeleteTbRow(id) {
if (confirm('آیا از حذف مطمئن هستید ؟')) {
$.get("/Machininfo/DeleteGallery/" + id,
function () {
$("#tbrow_" + id).hide('slow');
});
}
}
</script>
End Section
MetaData class:
Imports System.ComponentModel
Imports System.ComponentModel.DataAnnotations
Friend Class MachininfoDocsMetaData
<ScaffoldColumn(False)>
<Bindable(False)>
Public Property Id As Integer
Public Property MachininfoID As Integer
Public Property DocGroup As Integer
<Required(AllowEmptyStrings:=False, ErrorMessage:="لطفاً {0} را به درستی درج نمائید")>
<DisplayName("عنوان")>
<Display(Name:="عنوان")>
<StringLength(200, ErrorMessage:="مقدار {0} نباید بیش از 200 کاراکتر باشد")>
Public Property Title As String
<DisplayName("انتخاب فایل")>
<Display(Name:="انتخاب فایل")>
Public Property FileName As String
End Class
<MetadataType(GetType(MachininfoDocsMetaData))>
Partial Public Class MachininfoDocs
End Class
One common mistake is for who use mvc default bundles forget to import the bundle in the layout
#Scripts.Render("~/bundles/jqueryval")
Also you should activate them in the web.config appsettings
<appSettings>
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="ValidationSettings:UnobtrusiveValidationMode" value="none" />

I have MVC Error Parser Error

first time use MVC and i want to change the position and want to change the logo. after this i write the #using
the error is Parser Error what happened ?
#using Angga.Models
#model LoginViewModel
#{ ViewBag.Title = "Log in"; }
<div class="col-sm-4">
</div>
<div class="col-sm-4">
#using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { #class = "form-horizontal", role = "form"})) { #Html.AntiForgeryToken()
<div class="page animsition vertical-align text-center" data-animsition-in="fade-in" data-animsition-out="fade-out" style="animation-duration: 800ms; opacity: 1;">
<div class="page-content vertical-align-middle">
<div class="panel">
<div class="panel-body">
<div class="brand">
<img class="brand-img" src="~/image/logo-blue.png" alt="..." />
<h2 class="brand-text font-size-18">MYLOYALTY</h2>
</div>
<form method="post" action="customer_search.php" autocomplete="off">
#Html.ValidationSummary(true, "", new {#class = "text-danger"})
<div class="form-group form-material floating">
<!-- Untuk Email-->
<!-- <label class="floating-label">Email</label> -->
<div class="form-group">
#Html.LabelFor(m => m.Email) #Html.TextBoxFor(m => m.Email, new { #class = "form-control" }) #Html.ValidationMessageFor(m => m.Email, "", new { #class = "text-danger" })
</div>
#*
<input style="margin-left: auto; margin-right: auto; text-align: center;" type="email" class="form-control" name="email" />*#
</div>
<!-- Untuk Password-->
<div class="form-group form-material floating">
#Html.LabelFor(m => m.Password) #Html.PasswordFor(m => m.Password, new {#class= "form-control"}) #Html.ValidationMessageFor(m => m.Password, "", new { #class = "text-danger"}) #*
<input style="margin-left: auto; margin-right: auto; text-align: center;" type="password" class="form-control" name="password" />*#
</div>
<div class="form-group clearfix">
<div class="checkbox-custom checkbox-inline checkbox-primary checkbox-lg pull-left">
<input type="checkbox" id="inputCheckbox" name="remember" />
<label for="inputCheckbox">Remember me</label>
</div>
</div>
<button style="" class="btn btn-primary btn-block btn-lg" onclick="location.href='#Url.Action(" About ")'">Sign in</button>
</form>
</div>
</div>
}
<footer class="page-copyright page-copyright-inverse">
<p>© 2016. All RIGHT RESERVED.</p>
</footer>
</div>
</div>
</div>
<div class="col-sm-4">
</div>
#section Scripts { #Scripts.Render("~/bundles/jqueryval") }
and this my code :D, the error is Parser Error
The Error is
enter image description here
this arise for your nested form
because you already introduce a form in html div
#using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { #class = "form-horizontal", role = "form"})) { #Html.AntiForgeryToken()
........
and you try to introduce another form in the same form
<form method="post" action="customer_search.php" autocomplete="off">
......

object name is bind as prefix in input control

currently I am working in asp.net mvc view
I create form using below code.
#{
T4.Models.Order o = new T4.Models.Order();
}
#using (Html.BeginForm("Create", "Default5", FormMethod.Post))
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Order</h4>
<hr />
#Html.ValidationSummary(true)
<div class="form-group">
#Html.LabelFor(model => o.CustomerID, new {#class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model=>o.CustomerID)
#Html.ValidationMessageFor(model => o.CustomerID)
</div>
</div>
</div>
}
this code render perfectly but problem is it render with prefix of object's name.
show below html code
<input class="text-box single-line" data-val="true" data-val-required="The CustomerID field is required." id="o_CustomerID" name="o.CustomerID" type="text" value="">
you can see this html input control has id="o_CustomerID".I want that it only render with id="CustomerID".
I does not know why it takes object name as prefix.
please help me.
Thank You.
No need to initialize object of T4.Models.Order class.
#{
T4.Models.Order o = new T4.Models.Order();
}
Replace above code to:
#model T4.Models.Order
Then simple use:
<div class="form-group">
#Html.LabelFor(model => model.CustomerID, new {#class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model=> model.CustomerID)
#Html.ValidationMessageFor(model => model.CustomerID)
</div>
</div>

MVC Partial view to register button control action is Not applicable

I am use MVC Partial view .I have a two of Page AddPoll.cshtml and Listpoll.cshtml. page AddPoll.cshtml use Partial view.
I use AddPoll:partial view page to be placed a partial view with Listpoll.
#Html.Partial("/Areas/admin/Views/Poll/AddPoll.cshtml",new Mvc_baker.Areas.admin.Models.pollquestion());
AddPoll.cshtml
#model Mvc_baker.Areas.admin.Models.pollquestion
<link href="~/Content/bootstrapvalidform.css" rel="stylesheet" />
#using (Html.BeginForm(actionName: "AddPoll", controllerName: "Poll"))
{
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<form class="">
<div class="col-md-6">
<div class="panel panel-primary">
<label class="control-label">پرسش</label>
#Html.TextBoxFor(m => m.QuestionText, new { id = "Name",style="width:450px", #class = "form-control" })
#Html.ValidationMessageFor(m => m.QuestionText)
<input class="btn btn-primary btn-default" name="commit" value="register" type="submit">
</div>
</div>
</form>
}
When I click the register button control action is Not applicable
When the breakpoint action on control and I can not run
You seem to have 2 nested html forms which is invalid markup. The Html.BeginForm helper already generates a <form> tag. So you should get rid of the inner form.
#model Mvc_baker.Areas.admin.Models.pollquestion
<link href="~/Content/bootstrapvalidform.css" rel="stylesheet" />
#using (Html.BeginForm(actionName: "AddPoll", controllerName: "Poll"))
{
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="col-md-6">
<div class="panel panel-primary">
<label class="control-label">پرسش</label>
#Html.TextBoxFor(m => m.QuestionText, new { id = "Name",style="width:450px", #class = "form-control" })
#Html.ValidationMessageFor(m => m.QuestionText)
<input class="btn btn-primary btn-default" name="commit" value="register" type="submit" />
</div>
</div>
}

Resources