ajax.beginform() not working asp.net .netFramework 4.7.2 - asp.net-mvc

i have trouble with my code, ajax beginForm does not make reference with the controller event, i have all the library.
the view :
#using MyWebsite.Models
#model UsersCLS
#{
ViewBag.Title = "Index";
}
<h1 style="text-align:center;">My Web Site</h1>
<section class="vh-100">
<div class="container py-5 h-100">
<div class="row d-flex align-items-center justify-content-center h-100">
<div class="col-md-8 col-lg-7 col-xl-6">
<img src="https://mdbcdn.b-cdn.net/img/Photos/new-templates/bootstrap-login-form/draw2.svg" class="img-fluid" alt="Phone image">
</div>
<div class="col-md-7 col-lg-5 col-xl-5 offset-xl-1">
<form>
<!-- Email input -->
<div class="form-outline mb-4">
<input type="email" id="form1Example13" class="form-control form-control-lg" />
<label class="form-label" for="form1Example13">Email address</label>
</div>
<!-- Password input -->
<div class="form-outline mb-4">
<input type="password" id="form1Example23" class="form-control form-control-lg" />
<label class="form-label" for="form1Example23">Password</label>
</div>
<div>
<!-- Checkbox -->
<div class="form-check">
<input class="form-check-input"
type="checkbox"
value=""
id="form1Example3"
checked />
<label class="form-check-label" for="form1Example3"> Remember me </label>
</div>
</div>
<div>
Forgot password?
</div>
<!-- Submit button -->
<button type="submit" class="btn btn-primary btn-lg btn-block">Sign in</button>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg btn-block" data-bs-toggle="modal" data-bs-target="#exampleModal">
Check in
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
</div>
#using (Ajax.BeginForm("Add", "Login", null, new AjaxOptions
{
HttpMethod = "POST",
OnSuccess = "Create",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "error"
}))
{
<div>
#Html.Label("Name")
#Html.TextBox("name", null, new { #class = "form-control" })
</div>
<div>
#Html.Label("LastName")
#Html.TextBox("lastName", null, new { #class = "form-control" })
</div>
<div>
#Html.Label("Email")
#Html.TextBox("email", null, new { #class = "form-control", #type = "email" })
</div>
<div>
#Html.Label("UserName")
#Html.TextBox("userName", null, new { #class = "form-control" })
</div>
<div>
#Html.Label("Password")
#Html.Password("password", null, new { #class = "form-control" })
</div>
<button type="button" id="btnClose" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes </button>
}
<div id="error">
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</section>
<script src="~/Scripts/jquery-3.6.0.min.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
<script src="~/Scripts/bootstrap.js"></script>
this is the Model:
public class UsersCLS
{
[Display(Name ="Id")]
public int id { get; set; }
[Display(Name = "User")]
public string userName { get; set; }
[Display(Name = "Password")]
public string password { get; set; }
[Display(Name = "Name")]
public string name { get; set; }
[Display(Name = "Last Name")]
public string lastName { get; set; }
[Display(Name = "Email")]
public string email { get; set; }
}
And the Controller :
public ActionResult Index()
{
return View();
}
public string Add(UsersCLS usersCLS)
{
string answer = "";
if (!ModelState.IsValid)
{
}
else
{
using (var bd = new MyWebsiteEntities())
{
int count = 0;
using (var transc = new TransactionScope())
{
count = bd.Users.Where(p => p.UserName.Equals(usersCLS.userName)).Count();
if (count > 0)
{
answer += "<ul class='list-group'>";
answer += "<li></li>";
answer += "</ul>";
}
else
{
Users users = new Users();
users.UserName = usersCLS.userName;
SHA256Managed sha = new SHA256Managed();
byte[] bypass = Encoding.Default.GetBytes(usersCLS.password);
byte[] bycryptoPass = sha.ComputeHash(bypass);
string cryptoPass = BitConverter.ToString(bycryptoPass).Replace("-", "");
users.Password = cryptoPass;
users.Name = usersCLS.name;
users.UserName = usersCLS.userName;
users.LastName = usersCLS.lastName;
users.Email = usersCLS.email;
users.Enabled = true;
bd.Users.Add(users);
answer = bd.SaveChanges().ToString();
if (answer == "0")
{
answer = "";
}
transc.Complete();
}
}
}
}
return answer;
}
}
But (Ajax.BeginForm("Add", "Login")
does not make reference with controller event Please help, sorry for my bad english have a nice Day and thank you.

Related

Calling a view from different models in ASP.NET MVC

In my ASP.NET MVC application in the view, I'm calling another view that is not related to the current model. There I need some help that how to call the different model views from another view.
#model Asp_PASMVC.Models.VehicleService
#using Asp_PASMVC.Infrastructure
#{
ViewBag.Title = "View";
Layout = "~/Views/Shared/_Layout.cshtml";
List<SelectListItem> CompanyList = (List<SelectListItem>)TempData.Peek("ComapnyList");
List<SelectListItem> ReqTypes = (List<SelectListItem>)TempData.Peek("RequestTyleList");
List<SelectListItem> Employees = (List<SelectListItem>)TempData.Peek("EmployeeList");
List<SelectListItem> Location = (List<SelectListItem>)TempData.Peek("LocationList");
Asp_PASMVC.Models.AppRequest RequestDetails = (Asp_PASMVC.Models.AppRequest)TempData.Peek("RequestDetails");
}
#{
Html.RenderPartial("_MainRequestView", RequestDetails);
}
#using (Html.BeginForm("WorkshopUpdate", "VehicleService", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
#Html.HiddenFor(model => model.Req_Id)
#Html.AntiForgeryToken()
if (Model != null && Model.VehicleServiceApproveDetails != null)
{
foreach (Asp_PASMVC.Models.VehicleServiceApproveDetails Emp in Model.VehicleServiceApproveDetails)
{
Html.RenderPartial("_WorkshopUpdate", Emp);
}
}
<section class="content">
<div class="container-fluid">
<div class="row">
<div class="col-12">
<!-- Default box -->
<div class="card">
<div class="card-header">
<h3 class="card-title">Approver Details</h3>
<div class="card-tools">
<button type="button" class="btn btn-tool" data-card-widget="collapse" title="Collapse">
<i class="fas fa-minus"></i>
</button>
</div>
</div>
<div class="card-body">
<div>
<fieldset id="pnlApproverList" style="display:none">
<legend><h5>To whom you want to send this request for approval ? </h5> </legend>
<br />
<ul id="RequApprover" style="list-style-type: none">
#if (Model != null && Model.ApprovalPartyList != null)
{
foreach (Asp_PASMVC.Models.ApprovalParty Emp in Model.ApprovalPartyList)
{
Html.RenderPartial("_ApprovalView", Emp);
}
}
</ul>
<button type="button" id="addAnotherApprover" class="btn btn-success" href="#" onclick="this.style.display = 'none';">Add</button>
<script type="text/javascript">
$(function () {
// $("#movieEditor").sortable();
$("#addAnotherApprover").click(function () {
$.get('/VehicleService/AddApproverToReq', function (template) {
$("#RequApprover").append(template);
});
});
});
</script>
<br />
</fieldset>
</div>
</div>
<!-- /.card-footer-->
</div>
<!-- /.card -->
</div>
</div>
</div>
</section>
<div class="card-footer">
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Update and Sent" class="btn btn-success" />
</div>
</div>
</div>
}
<p>
#Html.ActionLink("Back to List", "Index")
</p>
So likewise here the model is VehicleService. So within that view, I want to call another view that is not within the vehicleservice model.
But I cannot load that partial view within this view. Is there any way to do this?
#model Asp_PASMVC.Models.ApprovalParty
#using Asp_PASMVC.Infrastructure
#{
string UserLvel = TempData.Peek("UserLevelClaims").ToString();
}
<li style="padding-bottom:15px">
#using (Html.BeginCollectionItem("ApprovalPartyList"))
{
<div class="row">
<div class="col-md-5 col-sm-5">
<div class="form-group">
<label>
#Html.RadioButtonFor(m => m.Approve_Type, false)
<span class="radiomargin">For Manager</span>
</label>
<br />
#if (UserLvel != "1")
{
<label>
#Html.RadioButtonFor(m => m.Approve_Type, true)
<span class="radiomargin">For Top Manager </span>
</label>
#Html.ValidationMessageFor(model => model.Approve_Type, "", new { #class = "text-danger" })
}
</div>
</div>
</div>
<br />
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="form-group row">
Select the Approver
<div class="col-sm-8">
#Html.DropDownListFor(model => model.Approver_Id, new List<SelectListItem>(), new { #id = "ddlEmployees", #class = "js-dropdown" })
#Html.ValidationMessageFor(model => model.Approver_Id, "", new { #class = "text-danger" })
</div>
</div>
</div>
</div>
}
</li>
Create a ViewModel which can have both Properties and pass that viewmodel to View
Model class:
public class VehicleSerivceViewModel
{
public VehicleService VehicleService { get; set; }
public ApprovalParty ApprovalParty { get; set; }
}
In View :
#model Asp_PASMVC.Models.VehicleServiceVewModel
pass ViewModel to partial as below:
#Model.ApprovalParty

Unable to pass RadioButton group selected value to Controller

I have a ViewModel defined as:
public class Fixtures
{
public int Id { get; set; }
public string HomeTeam { get; set; }
public string AwayTeam { get; set; }
public string HomeTeamCode { get; set; }
public string AwayTeamCode { get; set; }
public string League { get; set; }
public bool Home { get; set; }
public bool Draw { get; set; }
public bool Away { get; set; }
public string FixturePrediction { get; set; }
}
My view displays a list of these fixtures and for each fixture, I have a radio button grouping. I need some way of passing the selected radio button for each fixture to my Post method. My View and controller are as below:
View:
<div class="container">
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
for (int i = 0; i < Model.Count(); i++)
{
#Html.HiddenFor(model => model[i].Id)
<div class="col-sm-6">
<div class="panel panel-primary panel-fixture">
<div class="panel-heading text-center fixturetitle">
#Html.DisplayFor(model => model[i].HomeTeam) v #Html.DisplayFor(model => model[i].AwayTeam)
#Html.HiddenFor(model => model[i].HomeTeam)
#Html.HiddenFor(model => model[i].AwayTeam)
</div>
<div class="panel-body">
<div class="container-fluid">
<div class="row">
<div class="col-xs-4 text-center">
<img src="~/Content/Images/#string.Concat(Model[i].HomeTeamCode, ".png")" class="fixturecrest" />
<div class="text-center">
#Html.DisplayFor(model => model[i].HomeTeamCode)
#Html.HiddenFor(model => model[i].HomeTeamCode)
</div>
</div>
<div class="col-xs-4 text-center">
<label class="text-center">
V <br />
</label>
</div>
<div class="col-xs-4 text-center">
<img src="~/Content/Images/#string.Concat(Model[i].AwayTeamCode, ".png")" class="fixturecrest" />
<div class="text-center">
#Html.DisplayFor(model => model[i].AwayTeamCode)
#Html.HiddenFor(model => model[i].AwayTeamCode)
</div>
</div>
</div>
<hr />
<div class="row">
<div class="text-center" role="group" aria-label="Fixture Prediction">
<div class="col-xs-4 text-center">
<button type="button" class="btn btn-xs btn-default">
HOME
#Html.RadioButtonFor(model => model[i].FixturePrediction, Model[i].Home, new { #Name = Model[i].Id })
#Html.HiddenFor(model => model[i].FixturePrediction)
<span class="glyphicon glyphicon-check"></span>
</button>
</div>
<div class="col-xs-4 text-center">
<button type="button" class="btn btn-xs btn-default">
DRAW
#Html.RadioButtonFor(model => model[i].FixturePrediction, Model[i].Draw, new { #Name = Model[i].Id })
#Html.HiddenFor(model => model[i].FixturePrediction)
<span class="glyphicon glyphicon-check"></span>
</button>
</div>
<div class="col-xs-4 text-center">
<button type="button" class="btn btn-xs btn-default">
AWAY
#Html.RadioButtonFor(model => model[i].FixturePrediction, Model[i].Away, new { #Name = Model[i].Id })
#Html.HiddenFor(model => model[i].FixturePrediction)
<span class="glyphicon glyphicon-check"></span>
</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
}
<input type="submit" value="Confirm Predictions" class="btn btn-success" />
}
</div>
Post Method in Controller:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Test(IEnumerable<Fixtures> test)
{
if (ModelState.IsValid)
{
}
return RedirectToAction("Test");
}
My field FixturePrediction is always null and I have attempted several variations of the 'radiobuttonfor' shown above.
All I need is to assign a value to FixturePrediction based on which radiobutton is selected. What am I missing?
Thanks in advance.
I think what you are trying to do is this, I am not sure why you are doing it the way you are doing it, but this is how you would implement Html.RadioButtonFor, it takes the model prop, and the value, you can add as many buttons as you want, and the selected radio value will be passed as the value for your model prop
#Html.RadioButtonFor(model => model[i].FixturePrediction, "Home")
#Html.RadioButtonFor(model => model[i].FixturePrediction, "Draw")
#Html.RadioButtonFor(model => model[i].FixturePrediction, "Away")
Another example

Not getting the object values or formcollection values

I'm trying to verify user's login verification in MVC. I used a pop-up where it displays two forms as 1 for login and one for Register. I'm unable to get the object values or text box values of user inputs(username & password) even though its hitting the controller.
[HttpPost]
public ActionResult CheckUser(UserInfo obj)
{
int res = udaObj.CheckUser(obj.UserName, obj.Password);
if (res >= 1)
{
return RedirectToAction("Appointment", "Home");
}
else
{
//For testing purpose
return RedirectToAction("Appointment", "Home");
}
}
My cshtml is as follows :
#using (Html.BeginForm("RegisterUser", "Home", FormMethod.Post, new { id = "regForm" }))
{
<div class="tab-pane fade" id="signup">
<fieldset>
<!-- Sign Up Form -->
<!-- Text input-->
<div class="control-group">
<label class="control-label" for="Email">Email:</label>
<div class="controls">
#Html.TextBoxFor(x => x.Email, new { #class = "form-control input-large", #placeholder = "Joek#irawath.com", #required = "" })
</div>
</div>
<!-- Text input-->
<div class="control-group">
<label class="control-label" for="userid">Alias:</label>
<div class="controls">
#Html.TextBoxFor(x => x.UserName, new { #class = "form-control input-large", #placeholder = "Joek#irawath.com", #required = "" })
</div>
</div>
<!-- Password input-->
<div class="control-group">
<label class="control-label" for="password">Password:</label>
<div class="controls">
#Html.TextBoxFor(x => x.Password, new { #class = "form-control input-large", #placeholder = "********", #required = "", #type = "password" })
<em>1-8 Characters</em>
</div>
</div>
<!-- Text input-->
<div class="control-group">
<label class="control-label" for="reenterpassword">Re-Enter Password:</label>
<div class="controls">
<input id="reenterpassword" class="form-control" name="reenterpassword" type="password" placeholder="********" class="input-large" required="">
</div>
</div>
<!-- Button -->
<div class="control-group">
<label class="control-label" for="confirmsignup"></label>
<div class="controls">
<button id="btnconfirmsignup" type="submit" name="signin" class="btn btn-success">Sign Up</button>
#* <button id="btnsignin" type="submit" name="signin" class="btn btn-success">Sign In</button>*#
</div>
</div>
</fieldset>
</div>
}
#using (Html.BeginForm("CheckUser", "Home", FormMethod.Post, new { id = "loginForm" }))
{
<div class="tab-pane fade active in" id="signin">
<fieldset>
<!-- Sign In Form -->
<!-- Text input-->
<div class="control-group">
<label class="control-label" for="userid">Alias:</label>
<div class="controls">
#* <input required="" id="userid" name="userid" type="text" class="form-control" placeholder="JoeSixpack" class="input-medium" required="">*#
#Html.TextBoxFor(x => x.UserName, new { #class = "form-control input-large", #placeholder = "Joek#irawath.com", #required = "" })
</div>
</div>
<!-- Password input-->
<div class="control-group">
<label class="control-label" for="passwordinput">Password:</label>
<div class="controls">
#*<input required="" id="passwordinput" name="passwordinput" class="form-control" type="password" placeholder="********" class="input-medium">*#
#Html.TextBoxFor(x => x.Password, new { #class = "form-control input-large", #placeholder = "********", #required = "", #type = "password" })
</div>
</div>
<!-- Multiple Checkboxes (inline) -->
<div class="control-group">
<label class="control-label" for="rememberme"></label>
<div class="controls">
<label class="checkbox inline" for="rememberme-0">
<input type="checkbox" name="rememberme" id="rememberme-0" value="Remember me" style="margin-left: 0px">
Remember me
</label>
</div>
</div>
<div class="control-group">
<label class="control-label" for="confirmsignup"></label>
<div class="controls">
<button id="btnsignin" type="submit" name="signin" class="btn btn-success">Sign In</button>
#* <button id="btnsignin" type="submit" name="signin" class="btn btn-success">Sign In</button>*#
</div>
</div>
</fieldset>
</div>
}
UserInfo.cs
public class UserInfo
{
public int UserId { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public bool Gender { get; set; }
public DateTime? Dob { get; set; }
public string PhNumber { get; set; }
public string ImagePath { get; set; }
public DateTime? RegDate { get; set; }
public string Email { get; set; }
}
I do not see anything wrong with your code.
But what I can guess is that your controller does not follow the naming convention of controllers. Your controller name should have a "Controller" suffix.
In your case it should be
public class HomeController : Controller
{
....
}
Let me know if that helps

MVC data validation for listboxfor not working

I am having a bit of trouble with my form being validated even if my listboxfor is empty. This is the viewmodel im using:
public class CopyExcelReportSearchViewModel: IViewModel
{
[Required]
public DateRange DateRange { get; set; }
[Required]
public Guid CustomerId { get; set; }
[Required]
public List<Guid> ProjectIds { get; set; }
[Required]
public List<Guid> LocationsIds { get; set; }
public List<Customer> AvailableCustomers { get; set; }
public List<Project> AvailableProjects { get; set; }
public List<Location> AvailableLocations { get; set; }
public CopyExcelReportSearchViewModel()
{
ProjectIds = new List<Guid>();
LocationsIds = new List<Guid>();
AvailableCustomers = new List<Customer>();
AvailableProjects = new List<Project>();
AvailableLocations = new List<Location>();
}
}
Here is the html
#model Path.CopyExcelReportSearchViewModel
#{
var customerDropdown = Model.AvailableCustomers.Select(x => new SelectListItem
{
Text = x.Name,
Value = x.Id.ToString(),
Selected = Model.CustomerId == x.Id
});
var locationDropdown = Model.AvailableLocations.Select(x => new SelectListItem
{
Text = x.Name,
Value = x.Id.ToString(),
Selected = Model.LocationsIds.Any(y => y == x.Id)
});
var projectDropdown = Model.AvailableProjects.Select(x => new SelectListItem
{
Text = x.Name,
Value = x.Id.ToString(),
Selected = Model.ProjectIds.Any(y => y == x.Id)
});
}
<script>
var optionTemplate = [
'<option value="[VALUE]">[TEXT]</option>'
].join();
$(function () {
$('#CustomerId').change(function () {
var value = $(this).val();
$.ajax({
url: '#Url.Action("GetProjectsByCustomerId")?customerId=' + value,
datatype: 'json',
type: 'GET',
cache: false,
success: function (data) {
var projectSelect = $('#ProjectIds');
var options = '';
for (var i = 0; i < data.length; i++) {
options += optionTemplate.replace('[VALUE]', data[i].id).replace('[TEXT]', data[i].name);
}
projectSelect.empty().append(options).removeAttr('disabled').trigger('chosen:updated');
}
});
});
$('#CustomerId').trigger('change');
});
</script>
#using (Html.BeginForm("GenerateCopyReportExcel", "Report", FormMethod.Post))
{
//todo - Fix projectids validation
<div id="DlgStockReportSearch" class="modal fade" data-backdrop="static" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3>#Language.KPIReport</h3>
</div>
<div class="modal-body clearfix">
<div class="col-xs-12">
<div class="form-group">
<label for="CustomerId">#Language.Customer</label>
#Html.DropDownListFor(x => x.CustomerId, customerDropdown, new { #class = "form-control chosen", placeholder = Language.Customer })
#Html.ValidationMessageFor(x => x.CustomerId)
</div>
<div class="form-group required">
<label for="DateRange">#Language.Date</label>
#Html.EditorFor(x => x.DateRange)
</div>
<div class="form-group">
<label for="ProjectIds">#Language.Projects</label>
<div class="input-group button">
#Html.ListBoxFor(x => x.ProjectIds, projectDropdown, new { #class = "form-control chosen", multiple = ""})
#Html.ValidationMessageFor(m => m.ProjectIds)
<span class="input-group-addon">
<button type="button" class="btn btn-default chosen-select-all">Select all</button>
</span>
</div>
</div>
<div class="form-group">
<label for="LocationsIds">#Language.Location</label>
<div class="input-group button">
#Html.ListBoxFor(x => x.LocationsIds, locationDropdown, new { #class = "form-control chosen", multiple = "" })
<span class="input-group-addon">
<button type="button" class="btn btn-default chosen-select-all">Select all</button>
</span>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal" aria-hidden="true">#Language.Close</button>
<button type="submit" class="btn btn-primary">
#Language.GenerateReport
</button>
</div>
</div>
</div>
</div>
}
For some reason, I am able to submit the form even if i leave the listboxfor for projects and locations empty.

Why is my form validation not working when I have my validation summary in place

I have a #HTML.ValidationSummary within my form which is in a modal (dialog), however, it does not seem check if the form inputs have valid values before the modal closes. I will like the validation summary to be displayed, as shown below, my view model has validation attributes on it's properties.
#using (Html.BeginForm("Index", "Home", null, FormMethod.Post, new { Id = "frmSendEmail", #class = "form-horizontal" }))
{
<div class="modal fade" id="modalSendEmail" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="modalLabel">Email</h4>
</div>
<div class="modal-body">
#Html.ValidationSummary("Oops! Seems like we are missing some information, please provide details for the required field marked with a *", new { #class = "alert alert-danger" })
<div class="form-group">
<label for="txtName" class="col-sm-2 control-label">* Name:</label>
<div class="col-sm-10">
#Html.TextBoxFor(model => model.SenderName, null, new {id = "txtName", #class = "form-control", placeholder = "Name"})
#Html.ValidationMessageFor(model => model.SenderName)
</div>
</div>
<div class="form-group">
<label for="txtEmail" class="col-sm-2 control-label">* Email:</label>
<div class="col-sm-10">
#Html.TextBoxFor(model => model.SenderEmail, null, new { id = "txtEmail", #class = "form-control", placeholder = "Email", type = "email" })
#Html.ValidationMessageFor(model => model.SenderEmail)
</div>
</div>
<div class="form-group">
<label for="txtTelephone" class="col-sm-2 control-label">Telephone:</label>
<div class="col-sm-10">
#Html.TextBox("telephone", null, new { id = "txtTelephone", #class = "form-control", placeholder = "Telephone" })
</div>
</div>
<div class="form-group">
<label for="txtEnquiry" class="col-sm-2 control-label">* Enquiry:</label>
<div class="col-sm-10">
#Html.TextAreaFor(model => model.SenderEnquiry, new { id = "txtEnquiry", #class = "form-control", rows = "5" })
#Html.ValidationMessageFor(model => model.SenderEnquiry)
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-warning" id="btnSendEmail">Send</button>
</div>
</div>
</div>
</div>
}
**Model**
public class Enquiry
{
[Required]
public string SenderName { get; set; }
[Required]
public string SenderEmail { get; set; }
public string SenderTelephone { get; set; }
[Required]
public string SenderEnquiry { get; set; }
}
In the View, change
#Html.ValidationSummary("Oops! Seems like we are missing some information, please provide details for the required field marked with a *", new { #class = "alert alert-danger" })
to
#Html.ValidationSummary(true, "", new { #class = "alert alert-danger" })
In the controller:
public ActionResult Index(Enquiry emailEnquiry)
{
if (ModelState.IsValid)
{
// all done
return ...
}
ModelState.AddModelError("", "Something is invalid.");
return View(emailEnquiry); // don't forget returning with model
}

Resources