Form having dropdown country list when i click on submit button country ID value is passed to the controller..when i click on F10 keybord button country bind method is called .so again return to starting point so that again country value is Null Below i attached the screen shot:
<div class="page-content">
<div class="container-fluid">
<header class="section-header">
<div class="tbl">
<div class="tbl-row">
<div class="tbl-cell">
<h2>Company Registration Form</h2>
</div>
</div>
</div>
</header>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
<section class="tabs-section">
<div class="tabs-section-nav tabs-section-nav-icons">
<div class="tbl">
<ul class="nav" role="tablist">
<li class="nav-item">
<a class="nav-link active" href="#tabs-1-tab-1" role="tab" data-toggle="tab">
<span class="nav-link-in">
<i class="font-icon font-icon-cogwheel"></i>
Company Registration Form
</span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#tabs-1-tab-2" role="tab" data-toggle="tab">
<span class="nav-link-in">
<span class="glyphicon glyphicon-music"></span>
Company Social Network
</span>
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#tabs-1-tab-3" role="tab" data-toggle="tab">
<span class="nav-link-in">
<i class="fa fa-product-hunt"></i>
Company Reference
</span>
</a>
</li>
</ul>
</div>
</div><!--.tabs-section-nav-->
<div class="tab-content">
<div role="tabpanel" class="tab-pane fade in active show" id="tabs-1-tab-1">
<br />
<br />
<section>
<div>
<div class="row">
<div class="col-lg-4">
<fieldset class="form-group">
#Html.LabelFor(model => Model.company.CompanyName, new { #class = "form-label semibold control-label" })
#Html.TextBoxFor(model => model.company.CompanyName, new { #class = "form-control", #id = "txtCompanyName", placeholder = "Enter the Company Name" })
#Html.ValidationMessageFor(model => model.company.CompanyName)
</fieldset>
</div>
<div class="col-lg-4">
<fieldset class="form-group">
#Html.LabelFor(model => model.company.ShortName, new { #class = "form-label semibold" })
#Html.TextBoxFor(model => model.company.ShortName, new { #class = "form-control", #id = "txtShortName", placeholder = "Enter the Short Name" })
#Html.ValidationMessageFor(model => model.company.ShortName)
</fieldset>
</div>
<div class="col-lg-4">
<fieldset class="form-group">
#Html.LabelFor(model => model.company.Division, new { #class = "form-label semibold" })
#Html.TextBoxFor(model => model.company.Division, new { #class = "form-control", #id = "txtDivision", placeholder = "Enter the Division" })
#Html.ValidationMessageFor(model => model.company.Division)
</fieldset>
</div>
</div><!--.row-->
<br />
<div class="row">
<div class="col-lg-4">
<fieldset class="form-group">
#Html.LabelFor(model => model.company.Email, new { #class = "form-label semibold" })
#Html.TextBoxFor(model => model.company.Email, new { #class = "form-control", #id = "txtEmail", placeholder = "Enter your Email" })
#Html.ValidationMessageFor(model => model.company.Email)
</fieldset>
</div>
<div class="col-lg-4">
<fieldset class="form-group">
#Html.LabelFor(model => model.company.Address1, new { #class = "form-label semibold" })
#Html.TextBoxFor(model => model.company.Address1, new { #class = "form-control", #id = "txtAddress1", placeholder = "Enter your Address Line 1" })
#Html.ValidationMessageFor(model => model.company.Address1)
</fieldset>
</div>
<div class="col-lg-4">
<fieldset class="form-group">
#Html.LabelFor(model => model.company.Address2, new { #class = "form-label semibold" })
#Html.TextBoxFor(model => model.company.Address2, new { #class = "form-control", #id = "txtAddress2", placeholder = "Enter your Address Line 2" })
</fieldset>
</div>
</div><!--.row-->
<br />
<div class="row">
<div class="col-lg-4">
<fieldset class="form-group">
#Html.LabelFor(model => model.company.Country, new { #class = "form-label semibold" })
#Html.DropDownList("Country", null, "--- Select Country ---", new { #class = "select2-arrow" })
</fieldset>
</div>
<div class="col-lg-4">
<fieldset class="form-group">
#Html.LabelFor(model => model.company.State, new { #class = "form-label semibold" })
<select id="state" class="select2-arrow"></select>
</fieldset>
</div>
<div class="col-lg-4">
<fieldset class="form-group">
#Html.LabelFor(model => model.company.City, new { #class = "form-label semibold" })
<select id="city" class="select2-arrow"></select><br />
</fieldset>
</div>
</div><!--.row-->
<br />
<div class="row">
<div class="col-lg-4">
<fieldset class="form-group">
#Html.LabelFor(model => model.company.Pincode, new { #class = "form-label semibold" })
#Html.TextBoxFor(model => model.company.Pincode, new { #class = "form-control", #id = "txtPincode", placeholder = "Enter your Pincode" })
#Html.ValidationMessageFor(model => model.company.Pincode)
</fieldset>
</div>
<div class="col-lg-4">
<fieldset class="form-group">
#Html.LabelFor(model => model.company.CountryCode, new { #class = "form-label semibold" })
#*#Html.DropDownList("CountryCode", null, "---Select CountryCode---", new { #class = "select2-arrow" })*#
</fieldset>
</div>
<div class="col-lg-4">
<fieldset class="form-group">
#Html.LabelFor(model => model.company.MobileNo, new { #class = "form-label semibold" })
#Html.TextBoxFor(model => model.company.MobileNo, new { #class = "form-control", #id = "txtMobileNo", placeholder = "Enter your Mobile Number" })
#Html.ValidationMessageFor(model => model.company.MobileNo)
</fieldset>
</div>
</div><!--.row-->
<br />
<div class="row">
<div class="col-lg-4">
<fieldset class="form-group">
#Html.LabelFor(model => model.company.PhoneNo, new { #class = "form-label semibold" })
#Html.TextBoxFor(model => model.company.PhoneNo, new { #class = "form-control", #id = "txtPhoneNo", placeholder = "Enter your PhoneNo" })
</fieldset>
</div>
<div class="col-lg-4">
<fieldset class="form-group">
#Html.LabelFor(model => model.company.PanNo, new { #class = "form-label semibold" })
#Html.TextBoxFor(model => model.company.PanNo, new { #class = "form-control", #id = "txtPanNo", placeholder = "Enter Company PanNo" })
</fieldset>
</div>
<div class="col-lg-4">
<fieldset class="form-group">
#Html.LabelFor(model => model.company.TinNo, new { #class = "form-label semibold" })
#Html.TextBoxFor(model => model.company.TinNo, new { #class = "form-control", #id = "txtTinNo", placeholder = "Enter Company TinNo" })
</fieldset>
</div>
</div><!--.row-->
<br />
<div class="row">
<div class="col-lg-4">
<fieldset class="form-group">
#Html.LabelFor(model => model.company.GSTno, new { #class = "form-label semibold" })
#Html.TextBoxFor(model => model.company.GSTno, new { #class = "form-control", #id = "txtGSTno", placeholder = "Enter Company GSTno" })
</fieldset>
</div>
<div class="col-lg-4">
<fieldset class="form-group">
#Html.LabelFor(model => model.company.IECCode, new { #class = "form-label semibold" })
#Html.TextBoxFor(model => model.company.IECCode, new { #class = "form-control", #id = "txtIECCode", placeholder = "Enter Company IECCode" })
</fieldset>
</div>
</div><!--.row-->
<br />
<div class="row">
<div class="col-lg-4">
<fieldset class="form-group">
<label class="form-label semibold">Upload Company Logo</label>
<input type="file" name="file" id="txtUploadImage" style="cursor:pointer;" />
</fieldset>
</div>
<div class="col-lg-4">
<fieldset class="form-group">
<label class="form-label semibold">Perview Image</label>
<img id="image_upload_preview" src="http://placehold.it/100x100" alt="your image" />
<a id="remove" onclick="javascript:ClearFileUploadControl();" style="display: none; cursor: pointer;">Remove</a>
</fieldset>
</div>
</div>
<br />
<input type="submit" name="Submit" id="SaveCompany" value="Save" class="btn btn-rounded btn-inline btn-success" />
</div>
</section>
</div><!--.tab-pane-->
<div role="tabpanel" class="tab-pane fade" id="tabs-1-tab-2">
<br />
<section>
<div>
<div class="row">
<div class="col-lg-4">
<fieldset class="form-group">
#Html.LabelFor(model => model.CompanySocial.FaceBookID, new { #class = "form-label semibold" })
#Html.TextBoxFor(model => model.CompanySocial.FaceBookID, new { #class = "form-control", #id = "txtFaceBookID", placeholder = "Enter the Facebook Link" })
</fieldset>
</div>
<div class="col-lg-4">
<fieldset class="form-group">
#Html.LabelFor(model => model.CompanySocial.TwitterID, new { #class = "form-label semibold" })
#Html.TextBoxFor(model => model.CompanySocial.TwitterID, new { #class = "form-control", #id = "txtTwitterID", placeholder = "Enter the Twitter Link" })
</fieldset>
</div>
<div class="col-lg-4">
<fieldset class="form-group">
#Html.LabelFor(model => model.CompanySocial.linkedinID, new { #class = "form-label semibold" })
#Html.TextBoxFor(model => model.CompanySocial.linkedinID, new { #class = "form-control", #id = "txtlinkedinID", placeholder = "Enter the Linkedin Link" })
</fieldset>
</div>
</div><!--.row-->
</div>
</section>
<input type="submit" name="Submit" value="Previous" class="btn btn-rounded btn-inline btn-primary prev-step " />
<input type="submit" name="Submit" id="saveSocial" value="Next" class="btn btn-rounded btn-inline btn-success" />
</div><!--.tab-pane-->
<div role="tabpanel" class="tab-pane fade" id="tabs-1-tab-3">
Tab 3
<br />
<br />
<input type="submit" name="Submit" value="Previous" class="btn btn-rounded btn-inline btn-primary prev-step" />
<input type="submit" name="Submit" value="Finish" class="btn btn-rounded btn-inline btn-success" />
</div><!--.tab-pane-->
</div><!--.tab-content-->
</section><!--.tabs-section-->
}
</div>
</div>
Scripts:
<script>
function GetInfo() {
var Company = {
CompanyName: $("#txtCompanyName").val(), ShortName: $("#txtShortName").val(), Division: $("#txtDivision").val(), Email: $("#txtEmail").val(), Address1: $("#txtAddress1").val(), Address2: $("#txtAddress2").val(), Country: $("#Country").val(), State: $("#state").val(), City: $("#city").val(),
Pincode: $("#txtPincode").val(), MobileNo: $("#txtMobileNo").val(), PhoneNo: $("#txtPhoneNo").val(), PanNo: $("#txtPanNo").val(), TinNo: $("#txtTinNo").val(), GSTno: $("#txtGSTno").val(), IECCode: $("#txtIECCode").val()
};
var mainModel = {};
mainModel.Company = Company;
$.ajax({
type: "POST",
url: "/Company/AddCompany",
data: JSON.stringify(mainModel),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
}
function OnSuccess(response) {
alert(response.d);
}
$(document).ready(function () {
$("#saveSocial").click(function (e) {
e.preventDefault();
GetInfo1();
});
$("#SaveCompany").click(function (e) {
//e.preventDefault();
GetInfo();
});
});
function GetInfo1() {
var Social = { FaceBookID: $("#txtFaceBookID").val(), TwitterID: $("#txtTwitterID").val(), linkedinID: $("#txtlinkedinID").val() };
var mainModel = {};
mainModel.CompanySocial = Social;
$.ajax({
type: "POST",
url: "/Company/AddSocial",
data: JSON.stringify(mainModel),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function (response) {
alert(response.d);
}
});
}
</script>
<script>
$(document).ready(function () {
$("#Country").change(function () {
var id = $(this).val();
$("#state").empty();
$.get("State_Bind", { CountryID: id }, function (data) {
var v = "<option>--- Select State ---</option>";
$.each(data, function (i, v1) {
v += "<option value=" + v1.Value + ">" + v1.Text + "</option>";
});
$("#state").html(v);
});
});
$("#state").change(function () {
var id = $(this).val();
$("#city").empty();
$.get("City_Bind", { StateID: id }, function (data) {
var v = "<option>--- Select City---</option>";
$.each(data, function (i, v1) {
v += "<option value=" + v1.Value + ">" + v1.Text + "</option>";
});
$("#city").html(v);
});
});
});
</script>
Class value:
public int Country { get; set; }
public int State { get; set; }
public int City { get; set; }
Screen shot:
ActionResult:
[HttpPost]
public ActionResult AddCompany(MainModel mainModel)
{
try
{
Country_Bind();
if (ModelState.IsValid)
{
dp obj = new dp();
if (obj.AddNewCompany(mainModel))
{
ViewBag.Message = "Company added successfully";
}
}
return View();
}
catch
{
return View();
}
}
Country code:
public void Country_Bind()
{
DataSet ds = dblayer.Get_Country();
List<SelectListItem> coutrylist = new List<SelectListItem>();
foreach (DataRow dr in ds.Tables[0].Rows)
{
coutrylist.Add(new SelectListItem { Text = dr["CountryName"].ToString(), Value = dr["CountryID"].ToString() });
}
ViewBag.Country = coutrylist;
}
You are currently calling the Country_Bind() method as the first statement inside your httppost action method. You actually need to do that when you want to rerender the dropdown list when re rendering the same view.
If you are not doing an ajax post,You should follow the PRG pattern. On successful save of the data, you should redirect to the GET action method which render the view. If the Modelstate validation fails, that is when you return the same view (so that you can show the validation message to user) and you need to repopulate the dropdown data before that.
When you do a Redirect, ViewData does not work to transfer messages.Use TempData instead.
[HttpPost]
public ActionResult AddCompany(MainModel mainModel)
{
try
{
if (ModelState.IsValid)
{
dp obj = new dp();
if (obj.AddNewCompany(mainModel))
{
TempData.Message = "Company added successfully";
return RedirectToAction("CompanyList");
}
}
// We need to repopulate the data needed for rendering dropdown
Country_Bind();
return View(mainModel);
}
catch(Exception ex)
{
// to do : Make sure to log the error
return View("Error");
}
}
In your case, when you do an ajax post returning a redirect result does not makes sense. You may return the url to the next page if needed as part of a json response and use that in your ajax calls success/done event handler and do appropriate things (redirect to the new page/show a message to user)
[HttpPost]
public ActionResult AddCompany(MainModel mainModel)
{
try
{
if (ModelState.IsValid)
{
dp obj = new dp();
if (obj.AddNewCompany(mainModel))
{
return Json(new { Message="Company added successfully"});
}
}
return Json(new { Message="Validation errors!"});
}
catch(Exception ex)
{
// must log the exception
return Json(new { Message="Error "});
}
}
Make sure to update your success/done event handler of your ajax call to read the response (the json and do something like showing message to user/hiding or showing the next tab)
I also noticed an issue with your ajaxifying code. Since you are making an ajax form submission, you should prevent the default form submit behavior by calling the event.preventDefault method.
$("#SaveCompany").click(function (e) {
e.preventDefault();
GetInfo();
});
I would also recommend to use a flat-lean view model specific for the view. So your properties will not be nested level and when you use the helper methods (TextBoxFor etx), It will generate the correct input field names and you do not need to manually build the Js object you want to send. You may simply use the jquery serialize() method on your form object and send that as the data for the ajax call.
Related
Goal: I'm trying to make a page that has the user enter in an address, then click a button that will verify it with a FedEx API. With the new verified address (now with the extra postal code from FedEx), I want to have the user verify that the new address is correct using a modal popup all without reloading the page.
Issue: I'm having a problem where the partial isn't showing and I'm not sure what I'm doing wrong.
Here's the View:
#model AirmotionEcommerceWebsite.Models.Home.DeliveryAddressModel
#{
ViewBag.Title = "Shipping Address";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<br />
<div class="container">
#Html.AntiForgeryToken()
<form class="form-horizontal">
<h4>Shipping Address</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<partial id="ValidateAddress"></partial>
<div class="form-group">
<h5>Name</h5>
<div class="col-md-10">
#Html.EditorFor(model => model.strName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.strName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<h5>Attention To</h5>
<div class="col-md-10">
#Html.EditorFor(model => model.strAttnTo, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.strAttnTo, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<h5>Street</h5>
<div class="col-md-10">
#Html.EditorFor(model => model.strStreet1, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.strStreet1, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<h5>Street 2</h5>
<div class="col-md-10">
#Html.EditorFor(model => model.strStreet2, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.strStreet2, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<h5>City</h5>
<div class="col-md-10">
#Html.EditorFor(model => model.strCity, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.strCity, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#{
IEnumerable<SelectListItem> dataItems = ViewBag.states;
}
<div class="form-group">
<h5>State</h5>
<div class="col-md-10">
#Html.DropDownListFor(model => model.State.IntStateId, dataItems, "-- Select --", new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.State.IntStateId, "", new { #class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
<h5>Zip</h5>
<div class="col-md-10">
#Html.EditorFor(model => model.strZip, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.strZip, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<button type="button" class="btn btn-primary" data-ajax-method="get" data-toggle="ajax-modal" data-target="#ValidateAddress"
data-url="#Url.Action("GetValidationOnAddress", new { model = Model })">
Verify Address
</button>
</div>
</div>
</form>
</div>
<script>
$(function () {
var PlaceHolderElement = $('#ValidateAddress');
$('button[data-toggle="ajax-modal"]').click(function (event) {
event.preventDefault();
var url = $(this).data('url');
// get the form containing the submit button
var form = $(this).closest('form')
// serialize all the fields in the form
var model = form.serialize();
// the the request to the url along with the form (model) data
$.get(url, model).done(function (data) {
PlaceHolderElement.html(data);
PlaceHolderElement.find('.modal').modal('show');
$('#ValidateAddress').modal('show');
})
})
})
</script>
Here's the Controller:
[HttpGet]
public IActionResult GetValidationOnAddress(DeliveryAddressModel model)
{
FedexAPI fedexAPI = new FedexAPI();
List<string> listOfStreets = new List<string>();
listOfStreets.Add(model.strStreet1);
listOfStreets.Add(model.strStreet2);
var newAddress = fedexAPI.ValidateAddress(listOfStreets, model.strCity, model.State.StrStateCode, model.strZip, "US");
if (newAddress.customerMessages.Contains("INTERPOLATED.STREET.ADDRESS"))
{
// Address is not valid
model.ErrorMessage = "The address entered could not be found. Please double check your address. If issues perssist, please contact our office _________";
}
else
{
model.strStreet1 = newAddress.streetLinesToken[0];
if (newAddress.streetLinesToken.Count > 1)
model.strStreet2 = newAddress.streetLinesToken[1];
model.strCity = newAddress.city;
model.strZip = newAddress.postalCode;
}
return PartialView("_AddressValidationPartial", model);
}
And lastly here's the Partial View:
#model AirmotionEcommerceWebsite.Models.Home.DeliveryAddressModel
<div class="modal fade" id="ValidateAddress">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="ValidateAddressLabel">Validate Address</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form action="Create">
<div class="form-group">
#if (Model.ErrorMessage == null)
{
<h5>#Model.strName</h5>
#if (Model.strAttnTo != null)
{<h5>#Model.strAttnTo</h5>}
<h5>#Model.strStreet1</h5>
#if (Model.strStreet2 != null)
{<h5>#Model.strStreet2</h5>}
<h5>#Model.strCity</h5>
<h5>#Model.State.StrStateCode</h5>
<h5>#Model.strZip</h5>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Accept Changes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Edit</button>
</div>
}
else
{
<h4>#Model.ErrorMessage</h4>
}
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" data-save="modal">Save Changes</button>
</div>
</div>
</div>
</div>
I checked the console and found it was not able to find my partial view. Quick file move and it's working now! Thanks to #viveknuna for helping me notice this!
My Register.cshtml page contains both Patient and Doctor registration. I have written a script to check if the user is Patient then displays only information relevant to patient and vice-versa.
If click on register patient then patient information only should insert into the database and the vice-versa for doctor registration.
Currently, If I register patient then it should not take specialization data into database because it is doctor's specific information.
Another problem is that, If I change the specialization or city value from drop down then it throws error:
The ViewData item that has the key 'Specialization' is of type
'System.String' but must be of type 'IEnumerable'.
Exception Details: System.InvalidOperationException: The ViewData item
that has the key 'Specialization' is of type 'System.String' but must
be of type 'IEnumerable'.
Source Error: #Html.DropDownListFor(x
=> Model.Specialization, Model.Specializations, htmlAttributes: new { #class = "form-control form-control-sm" })
Register.cshtml
model Aayumitra.Models.RegisterViewModel
<!-- Register Form -->
<div id="accounts-form">
<div class="account-nav">
<div class="row">
<div class="col-md-12 text-center">
<ul class="d-flex align-items-center justify-content-center">
<li>#Html.ActionLink("Login", "Login", "Account", null, new { #class = "mr-4" })</li>
<li>#Html.ActionLink("Register", "Register", "Account", null, new { #class = "active" })</li>
</ul>
</div>
</div>
</div>
<script>
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function (m, key, value) {
vars[key] = value;
});
return vars;
}
var text = getUrlVars()["profile"];
let profile;
if (text) {
if (text == 'doctor') {
profile = 'doctor';
}
if (text == 'patient') {
profile = 'patient';
}
} else {
profile = 'patient';
}
$(document).ready(function () {
if (profile === "doctor") {
$(".doctor").show();
}
else {
$(".doctor").hide();
}
});
</script>
<div class="container">
<div class="row py-5 border-top">
<div class="col-md-6">
<div class="form-img text-right">
<img src="~/Content/images/accounts.png" class="img-fluid" alt="Accounts Image">
</div>
</div>
<div class="col-md-5">
<div class="card">
<div id="form-card-header" class="card-header d-flex justify-content-between bg-white">
<h6>Join Aayumitra </h6>
<span class="float-right">Are you a Doctor? <span>#Html.ActionLink("register here", "register", "Account", new { profile = "doctor" }, null)</span>
</div>
<script>
var formCardHeader = document.getElementById('form-card-header');
if(profile == 'patient'){
formCardHeader.innerHTML = `
<h6>Join Aayumitra </h6>
<span class="float-right">Are you a Doctor? <span>#Html.ActionLink("register here", "register", "Account", new { profile="doctor"}, null)</</span>
`
}
if (profile == 'doctor'){
formCardHeader.innerHTML = `
<h6 id="join-doctors">Join 1000+ doctors</h6>
<span id="not-doctor" class=""text-primary>#Html.ActionLink("Not a doctor?", "register", "Account", new { profile="patient"}, null)</span>
`
}
</script>
#using (Html.BeginForm("Register", "Account", FormMethod.Post, new { #class = "form-horizontal", role = "form" }))
{
<div class="card-body">
<form action="register.chtml?profile=doctor">
<div class="form-group">
<label for="FullName">First Name</label>
#Html.TextBoxFor(m => m.FirstName, new { #class = "form-control form-control-sm", placeholder = "First Name" })
</div>
<div class="form-group">
<label for="FullName">Last Name</label>
#Html.TextBoxFor(m => m.LastName, new { #class = "form-control form-control-sm", placeholder = "First Name" })
</div>
<div class="form-group">
<label for="email">Email</label>
#Html.TextBoxFor(m => m.Email, new { #class = "form-control form-control-sm", placeholder = "Email ID" })
</div>
<div class="form-group doctor">
<label for="number">Specialization</label>
#Html.DropDownListFor(x => Model.Specialization, Model.Specializations, htmlAttributes: new { #class = "form-control form-control-sm", placeholder = "Specialization" })
</div>
<div class="form-group">
<label for="number">City</label>
#Html.DropDownListFor(x => Model.City, Model.Cities, htmlAttributes: new { #class = "form-control form-control-sm", placeholder = "City" })
</div>
<div class="form-group">
<label for="number">Mobile Number</label>
<div class="col-md-8">
<div class="form-group">
#Html.TextBoxFor(m => m.MobileNumber, new { #class = "form-control form-control-sm", placeholder = "Mobile Number" })
</div>
</div>
</div>
<div class="form-group">
<label for="password">Create Password</label>
#Html.PasswordFor(m => m.Password, new { #class = "form-control form-control-sm", placeholder = "Password" })
</div>
<div class="form-group">
<input type="submit" value="Send" class="btn btn-primary btn-block">
</div>
</form>
</div>
}
</div>
</div>
</div>
</div>
</div>
<!-- Register Form End -->
Controller
// GET: /Account/Register
[AllowAnonymous]
public ActionResult Register()
{
var model = new RegisterViewModel();
List<SelectListItem> SpecialitiesList = GetSpecialities();
List<SelectListItem> CitiesList = GetCities();
model.Specializations = SpecialitiesList;
model.Cities = GetCities();
return View(model);
}
//
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email,FirstName=model.FirstName,LastName=model.LastName,City=model.City, IsDoctor = model.IsDoctor, NMC_Number = model.NMC_Number, Specialization = model.Specialization};
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
return RedirectToAction("Index", "Home");
}
AddErrors(result);
}
return View(model);
}
I think the best way to solve it write two register action(doctor,Patient)that doctor and Patient have own view
I have a form for adding Books in a database,
I Have a modal window in my form to create a Publisher, if the specified format doesn't exist in Publisher Dropdown, I Create my Modal in a partial view for add Publisher,
this is my view:
#model WebApplication3.Models.BookModel
#{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div id="CustomerList"></div>
<h2>Create New Book</h2>
<label class="text-#ViewBag.ClassName">
#ViewBag.Message
</label>
#using (Html.BeginForm("CreateBook", "Book", FormMethod.Post))
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.TitleID, "TitleID", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("TitleID", null, "Select a Title", htmlAttributes: new { #class = "form-control" })
#Html.ActionLink("Add New", "Create", "Title")
#Html.ValidationMessageFor(model => model.TitleID, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.FormatID, "FormatID", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("FormatID", null, "Select a Format", htmlAttributes: new { #class = "form-control" })
<a href="" class="" data-toggle="modal" data-target="#FormatModal">
Add New
</a>
#Html.ValidationMessageFor(model => model.FormatID, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ISBN, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.ISBN, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.ISBN, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Quantity, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Quantity, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Quantity, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</div>
</div>
}
<div class="modal fade" id="PublisherModal" tabindex="-1" role="dialog" aria-labelledby="publisherModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
#Html.Partial("_Publisher")
</div>
</div>
</div>
<div>
#Html.ActionLink("Back to List", "Index")
</div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
#Scripts.Render("~/bundles/jqueryajax")
}
and this is my Publisher partial view:
#model WebApplication3.Models.PublisherModel
#using (Ajax.BeginForm("CreatePublisher", "Book", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "publisherForm", InsertionMode = InsertionMode.ReplaceWith }))
{
<div id="publisherForm">
<div class="modal-header">
<h5 class="modal-title" id="publisherModalLongTitle">Create New Publisher</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
#Html.AntiForgeryToken()
<div class="form-horizontal">
<div class="form-group">
<span class="text-#ViewBag.ClassName">
#ViewBag.Message
</span>
</div>
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.Value, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Value, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Value, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.Description, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.Description, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.Description, "", new { #class = "text-danger" })
</div>
</div>
</div>
</div>
<div class="modal-footer">
#Html.ActionLink("Manage", "Index", "Publisher", new { area = "" }, new { #class = "btn btn-link" })
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</div>
}
and this is my controller
[HttpPost]
[ValidateAntiForgeryToken]
public PartialViewResult CreatePublisher([Bind(Include = "ID,Value,Description")] PublisherModel publisherModel)
{
ViewBag.TitleID = new SelectList(repoTitle.GetModels(), "ID", "Title");
ViewBag.FormatID = new SelectList(repoFormat.GetModels(), "ID", "Value");
PublisherModel publisher = repoPublisher.GetModels().FirstOrDefault(x => x.Value == publisherModel.Value);
if (publisher == null)
{
if (ModelState.IsValid)
{
repoPublisher.Insert(publisherModel);
ViewBag.PublisherID = new SelectList(repoPublisher.GetModels(), "ID", "Value", publisherModel.ID);
ViewBag.Message = $"Publisher \"{publisherModel.Value}\" Added Successfully";
ViewBag.ClassName = "success";
return PartialView("_Publisher");
}
}
else
{
ViewBag.Message = $"Publisher \"{publisherModel.Value}\" Already Exsisted";
ViewBag.ClassName = "danger";
}
ViewBag.PublisherID = new SelectList(repoPublisher.GetModels(), "ID", "Value");
return PartialView("_Publisher",publisherModel);
}
How Can change to my code that if a new Publisher inserted to Database, the Publisher dropdown refresh and select newly inserted data as a selected item?
About question
I can't run your code locally.
These operations can be implemented through JS.
I can show my tools.
This is the function gif:
Solution:
Select and add options as a whole.
// index.cshtml
<div>
#await Component.InvokeAsync("AutoAddListValue")
</div>
//Component
#model List<string>
<h3>Default page</h3>
<div>
<div>
<p>Lists</p>
<select name="select" class="" id="ComponentSelectId" onclick="ComponentSelectClick()">
<option id="ComponentOptionId" style="width:auto">ComponentTest</option>
#if (Model.Count != 0)
{
int count = Model.Count();
for (int item = 0; item < count; item++)
{
<option id="option">#Model[item].ToString()</option>
}
}
</select>
</div>
<div>
<input id="ComponentTestJsInput" class="d-none" value="132323" type="text" />
<input id="ComponentBtnDisplay" value="Add" type="button" class="btn btn-primary" onclick="ComponentBtnDisplayClick()">
<input id="ComponentBtnAdd" value="Add" type="button" class="btn btn-primary d-none" onclick="ComponentBtnAddClick()">
</div>
</div>
Bind data for components.
public class AutoAddListValueViewComponent : ViewComponent
{
private readonly SchoolContext _context;
public AutoAddListValueViewComponent(SchoolContext context)
{
_context = context;
}
public async Task<IViewComponentResult> InvokeAsync(
int maxPriority, bool isDone)
{
string MyView = "Default";
var items = await GetListAsync();
return View(MyView, items);
}
public async Task<List<string>> GetListAsync()
{
return await _context.Test.Select(o => o.Name).ToListAsync();
}
}
Using JS code to realize page interaction.
var ComponentBtnDisplayClick = function () {
$('#ComponentBtnAdd').removeClass("d-none");
$('#ComponentTestJsInput').removeClass("d-none");
$('#ComponentBtnDisplay').addClass("d-none");
}
var ComponentBtnAddClick = function () {
//alert("666666");
$('#ComponentBtnAdd').addClass("d-none");
$('#ComponentTestJsInput').addClass("d-none");
$('#ComponentBtnDisplay').removeClass("d-none");
inputVal = $('#ComponentTestJsInput').val();
//alert(inputVal);
$.ajax({
url: '/Test/SaveInputValue',
data: {
name: inputVal
},
type: 'post',
async: true,
cache: false,
success: function (data) {
//alert(data.code);
var option = "<option id='ComponentOptionId'>";
option += inputVal;
option += "</option>";
$("#ComponentSelectId").append(option);
var obj = document.getElementById("ComponentSelectId");
obj[obj.length - 1].selected = true;
},
error: function () {
alert('fail');
}
});
}
Other code
Source Code
I have a Radio button, that is preselected as No in the model. But I want it to disable a dropdown menu automatically. But if you select Yes then the dropdown works. I am aware that you can do it with JQuery. But I was wondering if its possible with C# MVC.
<div class="form-group dropdownSwitchContainer">
<label class="control-label col-md-2" for="TenantGroup">Is this a Proxy Organisation?</label>
<div class="col-lg-6">
<div class="admin-form theme-primary">
<div class="radio-custom radio-primary mt10 mr10 pull-left">
#Html.RadioButtonFor(m => m.ProxyOrganisation, true, new { #class = "control-label col-md-4" })
<label for="ProxyYes">Yes</label>
</div>
<div class="radio-custom radio-primary mt10 mr10 pull-left">
#Html.RadioButtonFor(m => m.ProxyOrganisation, true, new { #class = "control-label col-md-4" })
<label for="ProxyYes">No</label>
</div>
</div>
</div>
</div>
#{
var emptyList = new List<SelectListItem>();
var proxiesList = new SelectList(emptyList);
if (proxies != null)
{
proxiesList = new SelectList(proxies, "key", "value", 0);
}
}
<div class="form-group">
<label class="control-label col-md-2" for="AlternativeShippingTarget">Alternative Shipping Target</label>
<div class="col-md-8">
#Html.DropDownListFor(x => x.ShippingTarget, proxiesList, "Choose a target...", new { id = "targetSelect", #class = "form-control", placeholder = "Choose a target..." })
#Html.ValidationMessageFor(m => m.ShippingTarget, string.Empty, new { #class = "text-danger" })
</div>
</div>
</div>
Controller
`var model = new ViewModels.SocietiesCreateModel
{
SocietyName = string.Empty,
CISACCode = string.Empty,
ShippingTarget = string.Empty,
ProxyOrganisation = false,
PagingInfo = new ViewModels.PagingModel
{
Page = page,
Take = take,
SortAscending = sortAsc,
SortPropertyName = sortBy
}
};`
Is this what you mean?
<form action="">
<label for="no">
No
<input checked id="no" name="option" type="radio" id="no"/>
</label>
<label for="yes">
Yes
<input name="option" type="radio" id="yes"/>
</label>
<select id="dropdown">
<option value="1">option 1</option>
<option value="2">option 2</option>
</select>
</form>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
function toggleDropdown(){
if ($("#no").is(":checked")) {
$('#dropdown').prop('disabled', true);
} else {
$('#dropdown').prop('disabled', false);
}
}
$(document).ready(function(){
toggleDropdown();
});
$('input').change(function(){
toggleDropdown();
});
</script>
I would do it with JQuery
but you could try something like this without Jquery
<div class="form-group">
<label class="control-label col-md-2" for="AlternativeShippingTarget">Alternative Shipping Target</label>
<div class="col-md-8">
#{
object htmlAttributes;
if (Model.ProxyOrganisation)
{
htmlAttributes = new { id = "targetSelect", #class = "form-control", placeholder = "Choose a target..." };
}
else
{
htmlAttributes = new { id = "targetSelect", #class = "form-control", placeholder = "Choose a target...", disabled = "disabled" };
}
#Html.DropDownListFor(x => x.ShippingTarget, proxiesList, "Choose a target...", htmlAttributes)
#Html.ValidationMessageFor(m => m.ShippingTarget, string.Empty, new { #class = "text-danger" })
}
</div>
</div>
I want to post my data to the action which is HttpPost. I always do it but this time I don't know what happens here. Submit button sends view data to Get action (I mean Detail action which is HttpGet) the button type is submit as you see and in the Html.BeginForm as you see I have used FormMethod.Post but just sends dar. I searched here but found nothing.
View:
#using MF_Urmia.Models.ViewModels
#using Kendo.Mvc.UI
#model CourseVm
#{
Layout = "~/Areas/Admin/Views/Shared/_LayoutPage.cshtml";
}
#using(Html.BeginForm("Detail", "Course", FormMethod.Post))
{
#Html.HiddenFor(m => m.CourseId)
#Html.ValidationSummary(true)
<div class="form-column">
<h1 class="form-title-box">
<span class="form-title">
#ViewBag.title
</span>
</h1>
<div class="frm-group">
<div class="form-label form-required">
#Html.LabelFor(m => m.CourseName)
</div>
<div class="form-field">
#Html.TextBoxFor(m => m.CourseName, new { #class = "k-textbox" })
</div>
</div>
<div class="frm-group">
<div class="form-label form-required">
#Html.LabelFor(m => m.TypeName)
</div>
<div class="form-field">
#(Html.Kendo().DropDownListFor(m => m.CourseTypeId)
.BindTo((IEnumerable<SelectListItem>)ViewBag.CourseTypeList)
.OptionLabel("(Select One)")
.DataValueField("Value")
.DataTextField("Text")
.HtmlAttributes(new { id = "CourseType" }))
</div>
</div>
<div class="frm-group">
<div class="form-label form-required">
#Html.LabelFor(m => m.Price)
</div>
<div class="form-field">
#Html.TextBoxFor(m => m.Price, new { #class = "k-textbox", #type = "number"})
</div>
</div>
<br />
<div class="frm-group">
<div class="form-label form-required">
#Html.LabelFor(m => m.Copacity)
</div>
<div class="form-field">
#Html.TextBoxFor(m => m.Copacity, new { #class = "k-textbox", #type = "number" })
</div>
</div>
<div class="frm-group">
<div class="form-label form-required">
#Html.LabelFor(m => m.TeacherName)
</div>
<div class="form-field">
#Html.TextBoxFor(m => m.TeacherName, new { #class = "k-textbox" })
</div>
</div>
<div class="frm-group">
<div class="form-label form-required">
#Html.LabelFor(m => m.DurationTime)
</div>
<div class="form-field">
#Html.TextBoxFor(m => m.DurationTime, new { #class = "k-textbox", #type = "number" })
</div>
</div>
<br />
<div class="frm-group">
<div class="form-label form-required">
#Html.LabelFor(m => m.ClassStart)
</div>
<div class="form-field">
#Html.TextBoxFor(model => model.ClassStart, new { #class = "form-control kendo-date-picker", value = "", placeholder = "مثال :1396/05/20" })
</div>
</div>
<div class="frm-group">
<div class="form-label">
#Html.LabelFor(m => m.RegisterLimit)
</div>
<div class="form-field">
#Html.TextBoxFor(model => model.RegisterLimit, new { #class = "form-control kendo-date-picker", value = "", placeholder = "مثال :1396/05/20" })
</div>
</div>
<hr />
<div class="frm-group">
<div class="form-label form-required">
#Html.LabelFor(m => m.CourseImage)
</div>
<div class="form-field-2xl">
#Html.Kendo().Upload().Name("ImageFile").Messages(m => m.Select("select")).Multiple(false)
</div>
</div>
<hr />
<div class="frm-group">
<div class="form-label">
#Html.LabelFor(m => m.Description)
</div>
<div class="form-field full-width">
#Html.TextAreaFor(m => m.Description, new { #class = "k-textbox ck-editor-box" })
</div>
</div>
<hr />
<input value="submit" type="submit" class="btn btn-success form-btn" />
</div>
}
Controller:
public ActionResult Detail(int? id)
{
var courseId = id;
using (var db = new MFUEntities())
{
var courseTypeList = db.CourseType.ToList();
ViewBag.CourseTypeList = new SelectList(courseTypeList,
nameof(CourseType.CourseTypeId), nameof(CourseType.DisplayName));
if (courseId.HasValue)
{
//ويرايش
ViewBag.title = "Edit";
return View();
}
else
{
//ثبت
ViewBag.title = "Create";
return View();
}
}
}
[HttpPost]
public ActionResult Detail(CourseVm vm)
{
var courseId = vm.CourseId;
using (var db = new MFUEntities())
{
if (courseId > 0)
{
//Edit
var course = db.Course.FirstOrDefault(c => c.CourseId == vm.CourseId);
if (course == null)
{
return HttpNotFound();
}
//ويرايش عکس دوره
var oldImage = course.CourseImage;
//عکس قبلي را پاک کند
System.IO.File.Delete(Server.MapPath("/Images/CourseImage/Images/" + oldImage));
System.IO.File.Delete(Server.MapPath("/Images/CourseImage/Thumb/" + oldImage));
vm.CourseImage = Guid.NewGuid().ToString().Replace("-", "") +
Path.GetExtension(vm.ImageFile.FileName);
vm.ImageFile.SaveAs(Server.MapPath("/Images/CourseImage/Images/" + vm.CourseImage));
ImageResizer img = new ImageResizer();
img.Resize(Server.MapPath("/Images/CourseImage/Images/" + vm.CourseImage),
Server.MapPath("/Images/CourseImage/Thumb/" + vm.CourseImage));
Mapper.Map(vm, course);
}
else
{
//Create
//ثبت عکس دوره
vm.CourseImage = Guid.NewGuid().ToString().Replace("-", "") +
Path.GetExtension(vm.ImageFile.FileName);
vm.ImageFile.SaveAs(Server.MapPath("/Images/CourseImage/Images/" + vm.CourseImage));
ImageResizer img = new ImageResizer();
img.Resize(Server.MapPath("/Images/CourseImage/Images/" + vm.CourseImage),
Server.MapPath("/Images/CourseImage/Thumb/" + vm.CourseImage));
var course = new Course();
Mapper.Map(vm, course);
course.Remained = vm.Copacity;
course.CreateDate = DateTime.Now;
db.Course.Add(course);
}
db.SaveChanges();
return RedirectToAction("Index", "Course");
}
}