Hello,
My Creating View is not saving record to database, don't know what is the error, bcoz its showing no error but only invalidate Modelstate.
Controller code is below:
// GET: Discounts/Create
public ActionResult Create()
{
ViewBag.InstitutionID = new SelectList(db.tblInstitutes, "InstitutionID", "Institution");
ViewBag.FeeTypeID = new SelectList(db.tblFeeTypes, "FeeTypeID", "FeeType");
return View();
}
// POST: Discounts/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Discount discount)
{
if (ModelState.IsValid)
{
db.Discounts.Add(discount);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.InstitutionID = new SelectList(db.tblInstitutes, "InstitutionID", "Institution");
ViewBag.FeeTypeID = new SelectList(db.tblFeeTypes, "FeeTypeID", "FeeType");
return View(discount);
}
What I have tried:
View is here:
@model SchoolMVC.Models.Database.Discount
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Discount</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.InstitutionID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Institution", ViewBag.InstitutionID as SelectList, "-Select Institution-", new { @id = "InstitutionID", @class = "form-control" })
@Html.ValidationMessageFor(model => model.InstitutionID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.SessionID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Session", new SelectList(" "), "-Select Session-", new { @id = "SessionID", @class = "form-control" })
@Html.ValidationMessageFor(model => model.SessionID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ClassID, "ClassID", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Class", new SelectList(" "), "-Select Class-", new { @id = "ClassID", @class = "form-control" })
@Html.ValidationMessageFor(model => model.ClassID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.ClassFeeID, "ClassFeeID", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ClassFeeID, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ClassFeeID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FeeTypeID, "FeeTypeID", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("FeeTypeID", null,"-Select FeeType-", htmlAttributes: new { @class = "form-control" })
<Label id="ClassFee" class="text-left"> </Label>
@Html.ValidationMessageFor(model => model.FeeTypeID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EnrollID, "EnrollID", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Select Student", new SelectList(" "), new { @id = "EnrollID", @class = "form-control" })
@Html.ValidationMessageFor(model => model.EnrollID, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<Label class="col-md-2 text-primary">Percent: </Label> <input type="checkbox" name="Percent" id="Percent" class="form-check-input" />
</div>
<div class="form-group">
@Html.LabelFor(model => model.DiscountAmount, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DiscountAmount, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DiscountAmount, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.TotalDiscount, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.TotalDiscount, new { htmlAttributes = new { @readonly="readonly", @class = "form-control" } })
@Html.ValidationMessageFor(model => model.TotalDiscount, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DiscountPlus, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DiscountPlus, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DiscountPlus, "", 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>
<script src="~/Scripts/jquery-3.5.1.js"></script>
<script>
$(function () {
$("#InstitutionID").change(function () {
$.get("/Students/GetSessions", { InstitutionID: $("#InstitutionID").val() }, function (data) {
$("#SessionID").empty();
$("#SessionID").append("<option>-Select Session-</option>");
$.each(data, function (index, row) {
$("#SessionID").append("<option value='" + row.SessionID + "'>" + row.SessionName + "</option>");
});
});
});
});
$(function () {
$("#SessionID").change(function () {
$.get("/Students/GetClasses", { SessionID: $("#SessionID").val() }, function (data) {
$("#ClassID").empty();
$("#ClassID").append("<option>-Select Class-</option>");
$.each(data, function (index, row) {
$("#ClassID").append("<option value='" + row.ClassID + "'>" + row.ClassName + "</option>");
});
});
});
});
$(function () {
$("#ClassID").change(function () {
$.get("/Discounts/GetEnrollIDs", { ClassID: $("#ClassID").val() }, function (data) {
$("#EnrollID").empty().append("<option>-Select EnrollID-</option>");
$.each(data, function (index, row) {
$("#EnrollID").append("<option value='" + row.EnrollID + "'>" + row.StudentName + "</option>");
});
});
});
});
$(function () {
var calculateDiscount = function () {
var state = $('#Percent').is(":checked");
var disc = $("#DiscountAmount").val();
var feeamount = $('#ClassFee').text();
var discount = state ? (disc * feeamount) / 100 : feeamount - disc;
$('#TotalDiscount').val(discount);
};
$("#DiscountAmount,#Percent").change(calculateDiscount);
calculateDiscount();
});
$('#ClassID,#FeeTypeID').change(function () {
$.ajax({
url: '@Url.Action("GetClassFee", "Discounts")',
type: "POST",
dataType: "JSON",
contentType: "application/json",
data: JSON.stringify({ ClassID: $('#ClassID').val(), FeeTypeID: $('#FeeTypeID').val() }),
success: function (result) {
if (result.status == 'failed') {
$('#ClassFeeID').empty();
var txt = '<Label class=text-danger>No Fee Generated!</Label>';
$('#ClassFee').empty().append(txt);
}
if (result.status == 'success') {
$('#ClassFeeID').empty().val(result.ClassFeeID);
var fee = '<Label class=text-primary>' + result.ClassFee1 + '</Label>';
$('#ClassFee').empty().append(fee);
}
}
});
});
</script>