In my MVC project, I am working on a form that should be accessible based on certain conditions. The first view includes a dropdown list and a submit button. I want the value selected in the dropdown list to be passed and compared against a set condition. If the condition is not met, an alert should be shown instead of moving on to the form.
Below is the code snippet that I have implemented:
public ActionResult ChooseType()
{
var x = DataAccess.GetEmployee(@User.Identity.Name);
var lists = new SelectList(RuleAccess.GetAllRule(), "ID", "TypeDetail");
ViewBag.CategoryId = lists;
/*rule*/
ViewBag.comp1 = Logic.AnnualToogle(@User.Identity.Name);
if (x.EmpSex == "F" && x.EmpMaritalSt == "NIKAH")
{ ViewBag.comp2 = 1; }
else ViewBag.comp2 = 0;
return View();
}
[HttpGet]
public ActionResult Create(int lv_type)
{
var type = RuleAccess.GetTypeByID(lv_type);
ViewBag.type = type;
var model = new LeaveApplicationViewModels();
model.X = DataAccess.GetEmployee(@User.Identity.Name);
model.C = DataAccess.GetLeaveApp(@User.Identity.Name);
/*disable*/
ViewBag.dis = DataAccess.GetDisabledDate(@User.Identity.Name);
/*max*/
var max= RuleAccess.GetMaxByID(lv_type);
ViewBag.c = max;
if (lv_type == 1)
{
var used = RuleAccess.CountUsedAnnual(@User.Identity.Name);
var rem = max - used;
ViewBag.a = used;
ViewBag.b = rem;
}
else
{
ViewBag.b = max;
}
return View(model);
}
I have utilized the ViewBag.comp1 and ViewBag.comp2 in my view for validation:
<script type="text/javascript">
var x = @ViewBag.comp1;
var y = @ViewBag.comp2;
function validatecreate()
{
var value= document.getElementById("lv_type").value;
if (value==1)
{
if(x==1)
document.getElementById('validatecreate').submit();
else { alert('Action cant be done. You either have another annual leave application in pending status or you have reach the limit of annual leave'); }
}
else if(value==2)
{
if(y==1)
document.getElementById('validatecreate').submit();
else { alert('Action cant be done. You either are Male or Not Married Yet'); }
}
else if(value==3)
{
document.getElementById('validatecreate').submit();
}
else {
document.getElementById('validatecreate').submit();
//alert('Http Not Found');
}
}
@Html.DropDownList(
"lv_type", (SelectList) ViewBag.CategoryId,
"--Select One--",
new{ //anonymous type
@class = "form-control input-sm"
}
)
I have concerns about the URL manipulation where users can access the form directly by manually inputting the URL with a specific parameter value. Despite this, I still need the value of lv_type for my view. Any suggestions or guidance would be greatly appreciated.