I want to submit a single text field in a form. I have this view:
#model SenecaFormsServer.Areas.Dashboard.Models.EditProductModel
#using SenecaFormsServer.Areas.Dashboard.Models
#using Seneca.SfsLib.Connectors
<div id="ConnectorSettings-MAIL_PRODUCT_HANDLERS">
<h4>#Resources.Entity.Product.SettingsForConnector: Mail product handlers</h4>
#{
MailProductHandlersModel mailModel = Model.ConnectorModels["MAIL_PRODUCT_HANDLERS"] as MailProductHandlersModel;
}
<div class="form-group">
#Html.Label(Resources.Entity.Product.Sender, new { #class = "text-bold control-label col-md-2" })
<div class="col-lg-6 col-md-8 col-sm-10 ">
#Html.Editor("MailProductHandlers_sender", new { htmlAttributes = new { #class = "form-control", #Value = mailModel.Sender, #placeholder = Model.CustomerConfig.DefaultSenderAddress } })
</div>
</div>
<div class="form-group">
#Html.Label(Resources.Entity.Product.MailSubject, new { #class = "text-bold control-label col-md-2" })
<div class="col-lg-6 col-md-8 col-sm-10 ">
#Html.Editor("MailProductHandlers_subject", new { htmlAttributes = new { #class = "form-control", #Value = mailModel.Subject } })
</div>
</div>
<!--
<div class="form-group">
#Html.Label("Reply to form submitter", new { #class = "text-bold control-label col-md-2" })
<div class="col-lg-6 col-md-8 col-sm-10 ">
#Html.CheckBox("MailProductHandlers_replytoFormSubmitter", new { htmlAttributes = new { #class = "form-control" } })
</div>
</div>
-->
<div class="form-group">
#Html.Label(Resources.Entity.Product.MailMessage, new { #class = "text-bold control-label col-md-2" })
<div class="col-lg-6 col-md-8 col-sm-10 ">
#Html.EditorFor(model => mailModel.Message, new { htmlAttributes = new { #class = "form-control tiny-mce", data_lang = System.Globalization.CultureInfo.CurrentUICulture.Name } })
#Html.ValidationMessageFor(model => mailModel.Message)
#*#Html.TextArea("MailProductHandlers_message", mailModel.Message, new { #class = "form-control", #rows = 15 })*#
</div>
</div>
<div class="form-group">
#Html.Label(Resources.Entity.Product.GeneratePDF, new { #class = "text-bold control-label col-md-2" })
<div class="col-lg-6 col-md-8 col-sm-10 ">
#Html.Label(Resources.Entity.Product.GeneratePDFYes) #Html.RadioButtonFor(model => model.IsChecked, true, new { #checked = "checked", #class = "pdfchecker" })
#Html.Label(Resources.Entity.Product.GeneratePDFNo) #Html.RadioButtonFor(model => model.IsChecked, false, new { #class = "pdfchecker" })
</div>
</div>
<div class="form-group">
<div id="hideLable">
#Html.Label(Resources.Entity.Product.PdfMessage, new { #class = "text-bold control-label col-md-2" })
</div>
<div class="col-lg-6 col-md-8 col-sm-10 ">
#Html.EditorFor(model => mailModel.PdfMessage, new { htmlAttributes = new { #class = "form-control tiny-mce", #id = "mceu_61", data_lang = System.Globalization.CultureInfo.CurrentUICulture.Name } })
#Html.ValidationMessageFor(model => mailModel.PdfMessage)
</div>
</div>
#using (Html.BeginForm("GeneratePDFFromHtml", "Product", FormMethod.Post)) {
#Html.AntiForgeryToken()
<div class="col-xs-12 padding-bottom-10">
#*<i class="fa fa-fw fa-check"></i> #Resources.Action.Navigation.GeneratePDF *#
<button type="submit" value="Save" class="btn btn-success"><i class="fa fa-fw fa-check"></i> #Resources.Action.Navigation.GeneratePDF</button>
</div>
}
And I want to submit this part:
#using (Html.BeginForm("GeneratePDFFromHtml", "Product", FormMethod.Post)) {
#Html.AntiForgeryToken()
<div class="col-xs-12 padding-bottom-10">
#*<i class="fa fa-fw fa-check"></i> #Resources.Action.Navigation.GeneratePDF *#
<button type="submit" value="Save" class="btn btn-success"><i class="fa fa-fw fa-check"></i> #Resources.Action.Navigation.GeneratePDF</button>
</div>
}
and this is the action method:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult GeneratePDFFromHtml(EditProductModel model, string data, string designId)
{
string customerSchema = SfsHelpers.StateHelper.GetSchema();
//TemplateLibraryEntry entry = GetTemplateLibraryEntry(designId, customerSchema);
SubmittedForm sf = new SubmittedForm();
string schema = requestSchema;
customer_DbConnection db = new customer_DbConnection();
RenderFormController renderController = new RenderFormController();
renderController.GeneratePdf(data, db,sf);
//return RedirectToAction(model.DesignId, "Prdocut/Edit");
return Content("It works");
}
</div>
but the action method is not been triggered.
How to trigger the action method?
There has be created a PDF file from this textfield:
#using (Html.BeginForm("GeneratePDFFromHtml", "Product", null, FormMethod.Post, new { enctype = "multipart/form-data" })) {
#Html.AntiForgeryToken()
<div class="form-group">
<div id="hideLable">
#Html.Label(Resources.Entity.Product.PdfMessage, new { #class = "text-bold control-label col-md-2" })
</div>
<div class="col-lg-6 col-md-8 col-sm-10 ">
#Html.EditorFor(model => mailModel.PdfMessage, new { htmlAttributes = new { #class = "form-control tiny-mce", #id = "mceu_61", data_lang = System.Globalization.CultureInfo.CurrentUICulture.Name } })
#Html.ValidationMessageFor(model => mailModel.PdfMessage)
</div>
</div>
<div class="col-xs-12 padding-bottom-10">
<i class="fa fa-fw fa-check"></i> #Resources.Action.Navigation.GeneratePDF
#*<button type="submit" value="Save" class="btn btn-success"><i class="fa fa-fw fa-check"></i> #Resources.Action.Navigation.GeneratePDF</button>*#
</div>
}
and this is also the action method that has to been triggered and create the PDF file. But the method is not been triggered in the controller
The problem is that it is a partial view. So I solved like this:
Jquery:
$(document).ready(function () {
$('#btnGeneratePDF').click(function () {
var form = document.createElement("form");
form.setAttribute("GeneratePDFFromHtml", "post");
form.setAttribute("action", "GeneratePDFFromHtml");
form.setAttribute("target", "_blank");
var hiddenField = document.createElement("btnGeneratePDF");
//hiddenField.setAttribute("name", "search");
//hiddenField.setAttribute("value", "product1,product2");
hiddenField.setAttribute("type", "hidden");
form.appendChild(hiddenField);
document.body.appendChild(form);
form.submit();
});
});
and the view looks like now this:
<form method="post" id="form" name="form" action="#Url.Action("GeneratePDFFromHtml", "Product")" enctype="multipart/form-data">
<fieldset>
<div class="form-group">
<div id="hideLable">
#Html.Label(Resources.Entity.Product.PdfMessage, new { #class = "text-bold control-label col-md-2" })
</div>
<div class="col-lg-6 col-md-8 col-sm-10 ">
#Html.EditorFor(model => mailModel.PdfMessage, new { htmlAttributes = new { #class = "form-control tiny-mce", #id = "mceu_61", data_lang = System.Globalization.CultureInfo.CurrentUICulture.Name } })
#Html.ValidationMessageFor(model => mailModel.PdfMessage)
</div>
</div>
<div class="col-xs-12 padding-bottom-10">
#*<i class="fa fa-fw fa-check"></i> #Resources.Action.Navigation.GeneratePDF *#
<button type="submit" value="Create" class="btn btn-success" id="btnGeneratePDF" name="btnGeneratePDF" formtarget="_blank" onclick="GeneratePDFFromHtml()"><i class="fa fa-fw fa-check"></i> #Resources.Action.Navigation.GeneratePDF</button>
</div>
</fieldset>
</form>
but the url is now like this, after I press on the button:
http://localhost:51098/Dashboard/Product/Edit/GeneratePDFFromHtml?
what is ofcourse not correct. so how to trigger the method in the controller?
I get also this warning: Uncaught ReferenceError: GeneratePDFFromHtml is not defined
Related
I am trying to add a partial view [Create view] inside a details view, capturing the ID as parameter, but it always displays a different error.
I have been looking for a solution and the reason why it fails but there is no a good answer or good tutorial to make this possible.
Details view:
#model HumanForceMVC1._1.HF_Projects
#{
ViewBag.Title = "Details";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="slim-mainpanel">
<div class="container">
<div class="slim-pageheader">
<ol class="breadcrumb slim-breadcrumb">
<li class="breadcrumb-item">Home</li>
<li class="breadcrumb-item">Create new task</li>
</ol>
<!-- Modal -->
<div class="modal fade" id="addNewTaskModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
#*#Html.RenderPartial("AddTaskToProject", Model.HF_Tasks, new ViewDataDictionary { { Pid = Model.Id } })*#
#Html.Partial("AddTaskToProject", Model.HF_Tasks, new ViewDataDictionary { { "Pid", Model.Id } })
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
My controller:
[HttpPost]
public ActionResult AddTaskToProject(int Pid, HF_Tasks Item)
{
using (var context = new HumanForceDBEntities())
{
HF_Tasks myTask = new HF_Tasks();
myTask = Item;
myTask.ProjectAssigned = Pid;
myTask.TaskStatus = 2;
myTask.TaskDate = DateTime.Now;
context.HF_Tasks.Add(myTask);
context.SaveChanges();
return PartialView();
}
}
My Partial View:
#using (Html.BeginForm())
{ #Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>HF_Tasks</h4>
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.TaskName, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.TaskName, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.TaskName, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.TaskDescription, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.TaskDescription, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.TaskDescription, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.PersonAssigned, "PersonAssigned", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("PersonAssigned", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.PersonAssigned, "", 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-default" />
</div>
</div>
</div>
}
Please, somebody who can help me with this.
I appreciate in advance.
You need to add the #model ...HF_Tasks in the partial view on the top similar to what you have in the detail view.
I'm trying to submit my form using Ajax.beginform. I tried everything but i am getting this resource cannot found error.
I have two models - PatientEligibility and PatientDetails. Both contains 10-12 properties respectively.
I have created on viewmodel using those two models and I am trying to use some of the properties in one partial view and submitting that data button click
my view -
#model Portal.Models.EligibilityViewModel
<script type="text/javascript">
$(document).ready(function () {
$('input[type="checkbox"]').on('change', function () {
$('input[name="' + this.name + '"]').not(this).prop('checked', false);
});
});
</script>
<div class="row margin-top-0">
<div class="col-xs-12 col-sm-12">
<h4><b>Patient Eligibility</b></h4>
</div>
</div>
#using (Ajax.BeginForm("SubmitPatientEligibility", "Ophthalmic", new AjaxOptions { OnSuccess = "OnSuccess", OnFailure = "OnFailure" }))
{
<div class="row margin-top-10">
<div class="col-xs-12 col-sm-12">
<div class="margin-top-10">
<div class="checkbox">
<label>
#Html.CheckBoxFor(model => model.PatientEligibility.IsAboveSixty, new { #Name = "cbAgeEligibility", htmlAttributes = new { id = "cbAbvSixty", #class = "form-control", Name = "group1[]" } })
The patient is 60 or over
</label>
</div>
</div>
</div>
<div class="col-xs-12 col-sm-12">
<div class="margin-top-10">
<div class="checkbox">
<label>
#Html.CheckBoxFor(model => model.PatientEligibility.IsBelowSixteen, new { #Name = "cbAgeEligibility", htmlAttributes = new { id = "cbUnderSixteen", #class = "form-control", Name = "group1[]" } })
The patient is under 16
</label>
</div>
</div>
</div>
<div class="col-xs-12 col-sm-12">
<div class="margin-top-10">
<div class="checkbox">
<label>
#Html.CheckBoxFor(model => model.PatientEligibility.IsBetweenForty, new { #Name = "cbAgeEligibility", htmlAttributes = new { id = "cbAbovenForty", #class = "form-control", Name = "group1[]" } })
The patient is 40 or over and is the parent/brother/sister/child of a person who has or has had gloucoma
</label>
</div>
</div>
</div>
<div class="col-xs-12 col-sm-12">
<div class="margin-top-10">
<div class="checkbox">
<label>
#Html.CheckBoxFor(model => model.PatientEligibility.IsStudent, new { #Name = "cbAgeEligibility", htmlAttributes = new { id = "cbStudent", #class = "form-control", Name = "group1[]" } })
The patient is full time student aged 16, 17 or 18 at the establishment below.
</label>
</div>
</div>
</div>
<div class="col-xs-12 col-sm-12">
<div class="margin-top-10">
<div class="checkbox">
<label>
#Html.CheckBoxFor(model => model.PatientEligibility.IsPrisoner, new { data_toggle = "collapse", data_target = "#dvseenNotseen", htmlAttributes = new { id = "cbPrisoner", #class = "form-control" } })
The patient is prisoner on leave from the prison detailed below
</label>
</div>
</div>
</div>
</div>
<div class="col-xs-12 col-sm-12">
<div class="margin-top-10">
#Html.LabelFor(p => p.PatientBenefit.NINNumber)
#Html.EditorFor(model => model.PatientBenefit.NINNumber, new { htmlAttributes = new { #class = "form-control", autocomplete = "off" } })
</div>
</div>
<div class="col-xs-12 col-sm-12">
<div class="margin-top-10">
<label>DATE OF BIRTH</label>
#Html.TextBoxFor(model => model.PatientBenefit.DOB, new { #class = "form-control", autocomplete = "off", #id = "datetimepicker1" })
</div>
</div>
<div class="row margin-top-10">
<div class="col-xs-12 col-sm-3">
<button class="btn btn-default" name="btnBack">Previous</button>
</div>
<div class="col-xs-12 col-sm-9 text-right">
<button class="btn btn-default" name="btnBack">Save Draft</button>
<button class="btn btn-success fa fa-chevron-right icon-right" name="btnNext" type="submit">Next</button>
</div>
</div>
}
and my model is like this -
public class EligibilityViewModel
{
public PatientEligibility PatientEligibility { get; set; }
public PatientBenefit PatientBenefit { get; set; }
}
I am trying to call this method on submit of Next button -
public class OphthalmicController : Controller
{
[HttpPost]
public ActionResult SubmitPatientEligibility(EligibilityViewModel model)
{
return null;
}
}
i dont know why but i am getting this 500 error resource cannot found for this method.
What am I missing here?
I have a Form that creates an entry into my database. I am trying to add an additional form via a modal that will allow the user to populate the first form by searching a property on the webservice.
What I need is for when the user types hits search in the modal it will call an action on controller that then runs my web service client that then returns a list of appropriate information. I then want this list displayed as a table on the page. The user can then select which search result they want to use to which will then populate the original form. I think I can figure most of this out but wasnt sure of the best way to kick off the search from the modal. Code is as follows.
View
#model *******
#{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#Search">Populate From Service</button>
<!-- Modal -->
<div id="Search" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Search By Property</h4>
</div>
<div class="modal-body">
<form class="form-horizontal">
<p>blah blah blah</p>
<div class="form-group">
<label for="API14" class="control-label col-md-2">API</label>
<div class ="col-md-10">
<input type="text" class="form-control" id="API14" aria-describedby="API14" placeholder="Enter API">
</div>
</div>
</form>
</div>
<div class="modal-footer">
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Search" class="btn btn-success" />
<input type="button" value="Close" data-dismiss="modal" class="btn btn-danger" />
</div>
</div>
</div>
</div>
</div>
</div>
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
#Html.ValidationSummary(true, "", new { #class = "text-danger" })
<div class="form-group">
#Html.LabelFor(model => model.API14, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.API14, new { htmlAttributes = new { #class = "form-control"} })
#Html.ValidationMessageFor(model => model.API14, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.PROP_NO, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.PROP_NO, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.PROP_NO, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.PROP_NM, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.PROP_NM, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.PROP_NM, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ENTITY, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10 ">
#Html.EditorFor(model => model.ENTITY, new { htmlAttributes = new { #class = "form-control" } } )
#Html.ValidationMessageFor(model => model.ENTITY, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.PROD_ZONE_NM, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.PROD_ZONE_NM, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.PROD_ZONE_NM, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.LEASE_NAME, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.LEASE_NAME, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.LEASE_NAME, "", new { #class = "text-danger" })
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.WELL_NO, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.WELL_NO, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.WELL_NO, "", 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-success" />
</div>
</div>
</div>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
You could divide your issue into two steps.
Step 1: "What I need is for when the user types hits search in the modal it will call an action on controller that then runs my web service client that then returns a list of appropriate information. I then want this list displayed as a table on the page."
Add the following to your scripts section:
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
<script>
$(document).ready(function () {
$('#submitModal').click(function () {
var data = {
API14: $('#API14').val()
}
$.ajax({
url: '/Default/SearchByProperty',
data: data
}).success(function (html) {
$('#API14List').html(html);
$('#Search').modal('toggle');
});
});
});
</script>
}
This is a simple ajax call that takes the value from you modal input and submits to the server. Note the "url" parameter passed in the call as this will need to match your "/{controller}/{action}". Important: You will also need to update your "Search" button#API14 to type "button", not "submit", as this will avoid a postback which is undesired when making AJAX calls.
You can then add something like this to your controller:
public ActionResult SearchByProperty()
{
var model = new List<string>();
model.Add("one");
model.Add("two");
model.Add("three");
return PartialView(model);
}
... which will require the following in a partial view file I named SearchByProperty.cshtml:
#model List<string>
<table>
#foreach (var item in Model) {
<tr class="API14-item">
<td>#item</td>
</tr>
}
</table>
Step 2: "The user can then select which search result they want to use to which will then populate the original form. I think I can figure most of this out but wasnt sure of the best way to kick off the search from the modal. Code is as follows."
If you are confident in writing jQuery, then you should be able to write some js in the scripts section of your create page that will take the values you place in your table and populate your form:
$(document).on('click', '.API14-item', function () {
//
// TODO: your code here
//
});
Hope this helps!
I have project in which there is a form to edit user details. Initially it is disabled but when user search particular user and click edit button everything is enabled. I capture all changed data except dropdown list newly selected value. when user changed data and submit form. on the server end model property for dropdownlist contains old value in place new changed value.
Please help me
<script>
function Validate(strDecision) {
//alert("Validate");
debugger;
if (strDecision == "EDIT") {
if ($("#MerchDispName").val() == "" || $("#MerchDispName").val() == null) {
$("#DivInfoNErrorMsg").addClass("alert-danger");
$("#DivInfoNErrorMsg").html("<span>There is no data for editing.</span>");
$("#DivInfoNErrorMsg").show();
return false;
}
else {
debugger;
$(".ReadOnly").removeAttr("readOnly", "readOnly");
$(".Disabled").removeAttr("disabled", "disabled");
$("#Update").show();
$("#Edit").hide();
GetDetails();
}
}
else
{
var bFlag = false;
var iLengthPaymentMode = '#Model.dicPaymentModeVal.Count';
var iLengthProgramType = '#Model.dicProgramTypeVal.Count';
var i = 1;
for (; i <= iLengthPaymentMode; ) {
if ($("#PaymentCB" + i).is(":checked")) {
$("#PaymentCB" + i + "hid").val($("#PaymentCB" + i).val());
bFlag = true;
}
i++;
}
if (bFlag == false) {
$("#PaymentCBmsg").html("<span style='color:Red'>*Please select at least one payment option.</span>");
ShowErrorMsg("Please select at least one payment option.");
$("#PaymentCBmsg").show();
return false;
}
for (; i <= iLengthProgramType; ) {
if ($("#ProgramCB" + i).is(":checked")) {
$("#ProgramCB" + i + "hid").val($("#ProgramCB" + i).val());
bFlag = true;
}
i++;
}
if (bFlag == false) {
$("#ProgramCBmsg").html("<span style='color:Red'>*Please select at least one program type.</span>");
ShowErrorMsg("Please select at least one program type.");
$("#ProgramCBmsg").show();
return false;
}
else {
$(".Disabled").attr("disabled", "disabled");
$(".ReadOnly").attr("readOnly", "readOnly");
$("#Edit").show();
$("#Update").hide();
$("#frmMerchantEDIT").submit();
}
}
}
$(document).ready(function () {
//debugger;
var bFlagErrorMessage = '#Model.FlagErrorMessage';
var bFlagInfoMessage = '#Model.FlagInfoMessage';
$('#CountryList').on('change', function () {
debugger;
var id = this.value;
//$("#CountryID").val('0');
$("#CountryID").val(id); // or $(this).val()
});
$('#StateList').on('change', function () {
debugger;
var id = this.value;
//$("#StatesID").val('0');
$("#StatesID").val(id);
alert(id); // or $(this).val()
});
$('#CityList').on('change', function () {
debugger;
var id = this.value;
//$("#CityID").val('0');
$("#CityID").val(id);
//alert(this.value); // or $(this).val()
});
if (bFlagErrorMessage == 'True') {
debugger;
var sErrorMsg = '#Model.ErrorMessage';
$("#DivInfoNErrorMsg").addClass("alert-danger");
$("#DivInfoNErrorMsg").html("<span>" + sErrorMsg + "</span>");
$("#DivInfoNErrorMsg").show();
$(".ReadOnly").removeAttr("readOnly", "readOnly");
$(".Disabled").removeAttr("disabled", "disabled");
$("#Update").show();
$("#Edit").hide();
}
if (bFlagInfoMessage == 'True') {
debugger;
var sErrorMsg = '#Model.InfoMessage';
$("#DivInfoNErrorMsg").addClass("alert-success");
$("#DivInfoNErrorMsg").html("<span>" + sErrorMsg + "</span>");
$("#DivInfoNErrorMsg").show();
}
});
</script>
#using (Html.BeginForm("EDIT_MERCHANT", "MERCHANT_Permission", FormMethod.Post, new { #id = "frmMerchantEDIT", #class = "form-horizontal", #Role = "form" }))
{
#Html.AntiForgeryToken()
//#Html.ValidationSummary()
<div class="logmain_mechant">
<fieldset>
<legend class="panel-heading panel-primary" style="font-size:140%">Edit Merchant</legend>
<div id="DivInfoNErrorMsg" class="alert" hidden="hidden"></div>
<div class="wrapper">
<div class="panel-group" id="accordion">
<div class="panel panel-primary">
<div class="panel-heading">
<a class="accordion-toggle" style="text-decoration:none" data-toggle="collapse"
data-parent="#accordion" href="#collapseOne">
<h2 class="panel-title" style="color:White;font-size:135%">
Personal
<span class="indicator glyphicon glyphicon-chevron-down pull-right"></span>
</h2>
</a>
</div>
<div id="collapseOne" class="panel-collapse collapse in">
<div class="panel-body">
<div class="form-group">
<label for="MerchantName" class="control-label col-xs-8 col-sm-6 col-md-4">Merchant Name<span style="color:Red">*</span>:</label>
<div class="col-xs-12 col-sm-10 col-md-8">
#Html.TextBoxFor(m => m.MerchantName, new { #id = "MerchantName", #class = "form-control ReadOnly",#onclick= "ErrorDivHide()" })
#Html.ValidationMessageFor(m => m.MerchantName)
</div>
</div>
<div class="form-group">
<label for="MerchDispName" class="control-label col-xs-8 col-sm-6 col-md-4">Merchant Display Name<span style="color:Red">*</span>:</label>
<div class="col-xs-12 col-sm-10 col-md-8">
#Html.TextBoxFor(m => m.MerchantDisplayName, new { #id = "MerchDispName", #class = "form-control", #onclick="hideme()",#placeholder="Merchant Display Name" })
#Html.ValidationMessageFor(m => m.MerchantDisplayName)
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-8 col-sm-6 col-md-4">Address1<span style="color:Red">*</span>:</label>
<div class="col-xs-12 col-sm-10 col-md-8">
#Html.TextBoxFor(m => m.Address1, new { #id = "Address1", #class = "form-control ReadOnly", #onclick = "ErrorDivHide()" })
#Html.ValidationMessageFor(m => m.Address1)
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-8 col-sm-6 col- md-4">Address2:</label>
<div class="col-xs-12 col-sm-10 col-md-8">
#Html.TextBoxFor(m => m.Address2, new { #id = "Address2", #class = "form-control ReadOnly", #onclick = "ErrorDivHide()" })
#Html.ValidationMessageFor(m => m.Address2)
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-8 col-sm-6 col-md-4">Address3:</label>
<div class="col-xs-12 col-sm-10 col-md-8">
#Html.TextBoxFor(m => m.Address3, new { #id = "Address3", #class = "form-control ReadOnly", #onclick = "ErrorDivHide()" })
#Html.ValidationMessageFor(m => m.Address3)
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-8 col-sm-6 col-md-4">Country<span style="color:Red">*</span>:</label>
<div class="col-xs-12 col-sm-10 col-md-8">
#Html.DropDownListFor(m => m.CountryID, Model.liCountry, new { #id = "CountryList", #class = "form-control Disabled", #onchange = "GetStates()", #onclick = "ErrorDivHide()" })
#Html.HiddenFor(m => m.CountryID, new { #id="CountryId" })
#*#Html.TextBoxFor(m => m.CountryName, new { #id = "CountryName", #calss = "form-control" })*#
#Html.ValidationMessageFor(m => m.CountryID)
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-8 col-sm-6 col-md-4">State<span style="color:Red">*</span>:</label>
<div class="col-xs-12 col-sm-10 col-md-8">
#Html.DropDownListFor(m => m.StateID, Model.liState, new { #id = "StateList", #class = "form-control Disabled", #onchange = "GetCitis()", #onclick = "ErrorDivHide()" })
#Html.HiddenFor(m => m.StateID, new { #id = "StateIds" })
#*#Html.TextBoxFor(m => m.CountryName, new { #id = "CountryName", #calss = "form-control" })*#
#Html.ValidationMessageFor(m => m.StateName)
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-8 col-sm-6 col-md-4">City<span style="color:Red">*</span>:</label>
<div class="col-xs-12 col-sm-10 col-md-8">
#Html.DropDownListFor(m => m.CityID, Model.liCity, new { #id = "CityList", #class = "form-control Disabled", #onclick = "ErrorDivHide()" })
#Html.HiddenFor(m => m.CityID, new { #id = "CityIds" })
#*#Html.TextBoxFor(m => m.CountryName, new { #id = "CountryName", #calss = "form-control" })*#
#Html.ValidationMessageFor(m => m.CityName)
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-8 col-sm-6 col-md-4">ZipCode<span style="color:Red">*</span>:</label>
<div class="col-xs-12 col-sm-10 col-md-8">
#Html.TextBoxFor(m => m.ZipCode, new { #id = "ZipCode", #class = "form-control ReadOnly", #onclick = "ErrorDivHide()", #onkeypress = "return validateZipCode(event);" })
#Html.ValidationMessageFor(m => m.ZipCode)
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-10 col-sm-6 col-md-4">PG Merchant Name</label>
<div class="col-xs-12 col-sm-10 col-md-8">
#*type="text" name="PayControllerMerName" id="PayControllerMerName" class="form-control" />*#
#Html.TextBoxFor(m => m.sPGMerchantName, new { #id = "PayControllerMerName", #class = "form-control ReadOnly", #onclick = "ErrorDivHide()" })
#Html.ValidationMessageFor(m => m.sPGMerchantName)
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-8 col-sm-6 col-md-4">PG Merchant ID<span style="color:Red">*</span>:</label>
<div class="col-xs-12 col-sm-10 col-md-8">
#Html.TextBoxFor(m => m.PayControllerMerID, new { #name = "PayControllerMerID", #id = "PayControllerMerID", #class = "form-control disabled" })
#Html.ValidationMessageFor(m => m.PayControllerMerID)
</div>
</div>
#*<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</div>*#
</div>
</div>
</div>
<div class="panel panel-primary">
<div class="panel-heading">
<a class="accordion-toggle" data-toggle="collapse" style="text-decoration:none" data-parent="#accordion" href="#collapseTwo">
<h2 class="panel-title" style="color:White;font-size:135%">
Configuration
<span class="indicator glyphicon glyphicon-chevron-down pull-right"></span>
</h2>
</a>
</div>
<div id="collapseTwo" class="panel-collapse collapse">
<div class="panel-body">
<div class="form-group">
<label class="control-label col-xs-8 col-sm-6 col-md-4">Merchant Website<span style="color:Red">*</span>:</label>
<div class="col-xs-12 col-sm-10 col-md-8">
#Html.TextBoxFor(m => m.ReturnURL, new { #id = "ReturnURL", #class = "form-control ReadOnly", #onclick = "ErrorDivHide()" })
#Html.ValidationMessageFor(m => m.ReturnURL)
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-8 col-sm-6 col-md-4">Merchant EmailID<span style="color:Red">*</span>:</label>
<div class="col-xs-12 col-sm-10 col-md-8">
#Html.TextBoxFor(m => m.EmailID, new { #id = "EmailID", #class = "form-control ReadOnly", #onclick = "ErrorDivHide()" })
#Html.ValidationMessageFor(m => m.EmailID)
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-8 col-sm-6 col-md-4">Program Type<span style="color:Red">*</span>:</label>
<div class="col-xs-12 col-sm-10 col-md-8">
#{
var i = 1;
foreach (KeyValuePair<int, string> k in Model.dicProductType)
{
var ProductName = "ProgramCB" + i;
<span class="col-sm-offset-1 pull-left">
<input type="checkbox" name="ProgramCB" value="#k.Key"
checked="#Model.dicProgramTypeVal[k.Key]" id="#ProductName" class="Disabled"
onclick="ProgramClick(#ProductName)" /><label for="#ProductName" style="font-weight:normal;">#k.Value</label> </span>
i = i + 1;
ProductName = ProductName + "hid";
<input type="hidden" id="#ProductName" name="ProgramCB" />
}
}
</div>
<div hidden="hidden" id="ProgramCBmsg"></div>
</div>
<div class="form-group">
<label class="control-label col-xs-8 col-sm-6 col-md-4">Payment Mode<span style="color:Red">*</span>:</label>
<div class="col-xs-12 col-sm-10 col-md-8">
#{
i = 1;
foreach (KeyValuePair<int, string> k in Model.dicPaymentMode)
{
var elementName = "PaymentCB" + i;
<input type="checkbox" name="PaymentCB"
value="#k.Key" class="Disabled"
checked="#Model.dicPaymentModeVal[k.Key]"
id="#elementName" onclick="PaymentClick(#elementName)" />
<label for="#elementName"
style="font-weight:lighter;font-size:normal">#k.Value</label>
i = i + 1;
elementName = elementName + "hid";
<input type="hidden" id="#elementName" name="PaymentCB" />
}
}
</div>
#Html.HiddenFor(m => m.CurrentStateID, new { #id="StatesID"})
#Html.HiddenFor(m => m.CurrentCountryID, new { #id = "CountryID" })
#Html.HiddenFor(m => m.CurrentCityID, new { #id = "CityID" })
<div hidden="hidden" id="PaymentCBmsg"></div>
</div>
</div>
</div>
</div>
<div class="panel panel-default" id="Button">
<span style="color:Red">* Required Fields</span>
<div class="col-xs-12 col-sm-10 col-md-8 pull-right">
<button type="button" class="btn btn-default pull-right"
onclick="Validate('UPDATE')" id="Update">Update</button>
<button type="button" class="btn btn-default pull-right"
onclick="Validate('EDIT')" id="Edit">Edit</button>
</div>
</div>
<div hidden="hidden">
<button type="button" class="btn btn-default" id="btnClear" onclick="Clear();" style="display: none;">Clear</button>
</div>
</div>
</div>
</fieldset>
</div>
}
Server side code
if (objMerchant.CurrentCityID != 0 && objMerchant.CurrentCityID != objMerchant.CityID)
objMerchant.CityID = objMerchant.CurrentCityID;
if (objMerchant.CurrentStateID != 0 && objMerchant.CurrentStateID != objMerchant.StateID)
objMerchant.StateID = objMerchant.CurrentStateID;
if (objMerchant.CurrentCountryID != 0 && objMerchant.CurrentCountryID != objMerchant.CountryID)
objMerchant.CountryID = objMerchant.CurrentCountryID;
What i think going wrong is the Hidden control. Your code with dropdownlist contains
#DropDownListFor and #HiddenFor due to which there will be two input with same name and id, and when you are submitting your form. It is taking from the hidden input.
Try to remove the hidden control, because it is not correct to have same id for more than one element in html
I hope this will solve your porblem.
I have created a form using asp.net mvc 5 and when I click the submit button it sucessfully submiting the data. it means it successfully call Create method in my controller. I added
RedirectToAction("Index", "ExaminationManager");
code to last line of the code in Create method. But this not redirect to "Index". It always redirect to "Create".
This is my view code :
#model TutionWeb1.Models.ViewModels.ExaminationManagerVM.ExamViewModel
#using (Html.BeginForm("Create", "ExaminationManager", FormMethod.Post, new { id = "createForm" }))
{
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
#Html.AntiForgeryToken()
<div class="form-horizontal">
#Html.ValidationSummary(true)
<div class="form-group">
#Html.LabelFor(model => model.SubjectID, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownListFor(model => model.SubjectCategoryID, ViewBag.SubjectCategoriyID as SelectList, "Select a Subject Category", new { id = "SubjectCategory", #class = "form-control" })
#Html.ValidationMessageFor(model => model.SubjectCategoryID)
<br/>
#Html.DropDownListFor(model => model.SubjectID, Enumerable.Empty<SelectListItem>(), "Select a Subject", new { id = "SubjectID", #class = "form-control" })
#Html.ValidationMessageFor(model => model.SubjectID)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ExamName, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.TextBoxFor(model => model.ExamName, new { #class = "form-control", placeholder = "Exam Name" })
#Html.ValidationMessageFor(model => model.ExamName)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.ExamNote, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.TextBoxFor(model => model.ExamNote, new { #class = "form-control", placeholder = "Description" })
#Html.ValidationMessageFor(model => model.ExamNote)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.EnrollKey, new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.TextBoxFor(model => model.EnrollKey, new { #class = "form-control", placeholder = "Enrollment Key" })
#Html.ValidationMessageFor(model => model.EnrollKey)
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<input type="submit" class="btn btn-primary" value="Submit" />
</div>
</div>
</div>
</div>
}
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.0.min.js"></script>
<script type="text/jscript">
$(function () {
$('#SubjectCategory').change(function () {
$.getJSON('/ExaminationManager/SubjectsList/' + $('#SubjectCategory').val(), function (data) {
var items = '<option value="">Select a District</option>';
$.each(data, function (i, subject) {
items += "<option value='" + subject.Value + "'>" + subject.Text + "</option>";
});
$('#SubjectID').html(items);
});
});
});
</script>
Ajax is used to drop down cascade.
This is Create action code :
[HttpPost]
public void Create(ExamViewModel vm)
{
if (ModelState.IsValid)
{
Mapper.CreateMap<ExamViewModel, Examination>();
var exam = new Examination();
Mapper.Map<ExamViewModel, Examination>(vm, exam);
exam.TutorID = KEY_TUTOR_ID;
service_exam.CreateExam(exam);
RedirectToAction("Index", "ExaminationManager");
}
}
EDIT :
Index action:
public ActionResult Index(int? page)
{
var examinationdata = new ExaminationManagerViewModel();
ViewBag.SubjectCategoriyID = new SelectList(service_subject_category.GetSubjectCategories(KEY_LANG), "SubjectCategoryID", "SubjectCategoryName");
IEnumerable<SubjectsResult> subjects_mod = service_subject.GetSubjectsByTutor(KEY_TUTOR_ID,KEY_LANG);
Mapper.CreateMap<SubjectsResult, SubjectListViewModel>();
var subjects = Mapper.Map<IEnumerable<SubjectsResult>, List<SubjectListViewModel>>(subjects_mod);
examinationdata.Subjects = subjects;
IEnumerable<Examination> exams = service_exam.GetExams(KEY_TUTOR_ID).ToList();
Mapper.CreateMap<Examination, ExamRowViewModel>();
var attachments = Mapper.Map<IEnumerable<Examination>, List<ExamRowViewModel>>(exams);
examinationdata.Exam = new ExamViewModel();
examinationdata.Exams = attachments.ToList().ToPagedList(page ?? 1,3);
return View(examinationdata);
}
Its because your Create method is void and returns nothing.
You can try something like this to redirect:
Response.Redirect("/[ControllerName]/[ActionName]");
However, it would be preferable to stick to the MVC pattern,
public ActionResult Create(ExamViewModel vm)