Prevent Multiple Primary When the Checkbox is Checked - c#

I have a Form where I want to limit the Data from my Drop Downlist when the checkbox is checked
So when the Is Primary checkbox is Checked , There will be no 2 Email address that the Is Primary is Both True !! That is what i Want to Prevent
So that When i Save theres a Message window that will Appear and Let the user know that he/she checked the checkbox for the Same Drop Down Value
using (Html.BeginForm("Save", "Worker", FormMethod.Post, new { id = "contactForm" }))
{
<input type="hidden" id="workerId" name="workerId" value="#workerId" />
<p>
<label for="ddlContactType">
Contact Type</label>
<span>
#Html.DropDownList("ddlContactType", new SelectList(ViewBag.ContactTypeList, "ID", "Display_Value", workerContactType), "[Please Select]",
new Dictionary<string, object>
{
{"class","validate[required] inputLong"}
})
</span>
</p>
<p>
<label for="txtContactValue">
Contact Value</label>
<span>
<input type="text" id="txtContactValue" name="txtContactValue" class="validate[required] inputLong" value="" />
<input type="checkbox" name="chkWorkerIsPrimary" id="chkWorkerIsPrimary" value="true"
checked="checked" />
<label>
Is Primary?</label>
</span>
</p>
<p>
<span>
<input type="submit" id="btnAdd" class="styledButton" value="Add" />
</span>
</p>
}
</div>
I need the Validation written in Javascript or Jquery but i dont know how to start :(
Thanks

This is where you begin.
$('#chkWorkerIsPrimary :checkbox').click(function() {
var $this = $(this);
if ($this.is(':checked')) {
// do something
}
else {
//do something else
}
});

Related

incorrect radio button set as "checked"

Newbie question, please be kind.
I have this Index.cshtml page in C# MVC application. When the page is rendered, the last button is checked. How do I make the first button (All) set to checked instead?
I am reading thru the "similar questions", but none seem to address my question. Please help. Thank you in advance if you can explain this to me.
<h1>#ViewBag.title</h1>
<form action="/list/index" method="post">
<h2>Search by:</h2>
<p>
#foreach (var column in ViewBag.columns)
{
<span>
<input type="radio" name="searchType" id="#column.Key" value="#column.Key" checked="#column.Key == 'all'" />
<label for="#column.Key">#column.Value</label>
</span>
}
</p>
<p>
<label for="searchTerm">Keyword:</label>
<input type="text" name="searchTerm" id="searchTerm" />
</p>
<input type="submit" value="Search" />
</form>
<hr />
#if (ViewBag.service != null)
{
<table>
#foreach (var srv in ViewBag.service)
{
<tr>
<td>
<p>Provider: #srv.Provider.Name</p>
<p>Location: #srv.Location.Address</p>
<p>Category: #srv.Category.Name</p>
#* <p>Tag: #service.Tag</p>
<p>Keyword: #service.Keyword</p>*#
</td>
</tr>
}
</table>
}
In one condition:
<input type="radio" name="searchType" id="#column.Key" value="#column.Key" #(column.Key == 'all' ? "checked" : " ") />
Checked or not checked?
You can just use checked for checked any radio button:
<input type="radio" checked />
For conditionally check any of your radio button, use this:
#if(column.Key == 'all'){
<input type="radio" name="searchType" id="#column.Key" value="#column.Key" checked />
}else{
<input type="radio" name="searchType" id="#column.Key" value="#column.Key" />
}

How to pass an object ID from a button click in asp .net core 3

I want to pass an object ID from a cardView. item.vehicleID value should be passed to the create method of a controller. When I click the button it should also route me to following page "https://localhost:44367/User/Bookings/Create".
I have my code like this,
<div class="card-columns">
#foreach (var item in Model)
{
<div class="card">
<div class="card-body">
<img class="lazyload" src="#("~/VehicleImages/"+item.vehicleImageName)" asp-append-version="true" width="100%" height="100%" />
<h5 class="card-title">#item.vehicleType</h5>
<p class="card-text">Vehicle Availability: #item.vehicleAvailability</p>
<p class="card-text">Plate Number: #item.plateNumber</p>
<p class="card-text">Price/Day: #item.vehiclePricePerDay</p>
<p class="card-text">Transmission: #item.transmission</p>
<input type="submit" class="btn btn-info form-control" value="Book" onclick="location.href='#Url.Action("Areas=User", "Create", "BookingsController", new { id = item.vehicleID })'" />
</div>
</div>
}
</div>
What I actually want is, when I click the input button, I want to get the item.vehicleID and pass the ID value to the Create method in the BookingsController that I'm calling which is located in the User Folder and this User Folder is located in the Areas Folder.
The below code lines shows how I tried,
<input type="submit" class="btn btn-info form-control" value="Book" onclick="location.href='#Url.Action("Areas=User", "Create", "BookingsController", new { id = item.vehicleID })'" />
<input type="submit" class="btn btn-info form-control" value="Book" onclick="location.href='#Url.Action("Create", "BookingsController", "User", new { id = item.vehicleID })'" />
<input type="submit" asp-controller="Bookings" asp-area="User" asp-action="Create" class="btn btn-info form-control" value="Book" itemid="vehicleID" />
Currently I tried the above code lines but it doesn't load the page or anything. So what I actually want is to direct to this link "https://localhost:44367/User/Bookings/Create" when I click the button and it also should pass the vehicleID from item.vehicleID to the Create method of the BookingsController from that button click.

Dynamic column selection as per user input in asp.net mvc

I'm showing the user a screen where he can check what fields he wants to get from the database. Based on the checked checkboxes I want to write a query using LINQ or lambda expression and fetch the data in ASP.NET MVC. How to do that as column selections are dynamic? For example, if only Name and Email columns are checked then get the data of those 2 columns only from the database.
Controller
public ActionResult Report()
{
return View();
}
[HttpPost]
public ActionResult Report(Employee emp)
{
List<Employee> employees = new List<Employee>();
employees = db.Employees.Select(x => new Employee()
{
// select Only those columns which are checked by the user
}).ToList();
return View();
}
View
#model IEnumerable<DynamicCheck.Models.Employee>
#{
ViewBag.Title = "EmployeeReport";
}
<h2>EmployeeReport</h2>
<form action="~/Employees/Report" method="post">
<div>
<div class="col-lg-3">
<input type="checkbox" name="Name" />
<label>Employee Name</label>
</div>
<div class="col-lg-3">
<input type="checkbox" name="Email" />
<label>Employee Email</label>
</div>
<div class="col-lg-3">
<input type="checkbox" name="Address" />
<label>Employee Address</label>
</div>
<div class="col-lg-3">
<input type="checkbox" name="Phone" />
<label>Employee Name</label>
</div>
<input type="submit" value="Submit"/>
</div>
</form>
you should to use javascript and check what columns sent to view.
and create html elements dynamically...

Creating a cookie using radio buttons

This is now solved, leaving it here for others to find
I have tried to find the answer to the question, although I'm fairly new to this, I could have had the answer staring me in the face. So I do apologise.
I have created a MVC web app which basically saves a selection on a radio button to a cookie and then show's the selection on the next page. I think I'm close, but could you look at the code below and see why I can't get it to show the radio button text? Thanks in advance!
This is my controller;
public IActionResult WriteCookies(string submitOption, bool isPersistent)
{
if (isPersistent)
{
CookieOptions options = new CookieOptions();
options.Expires = DateTime.Now.AddDays(14);
Response.Cookies.Append("setting", submitOption, options);
}
else
{
Response.Cookies.Append("setting", submitOption);
}
ViewBag.Message = "Turn Registered!";
return View("Meoryou");
}
public IActionResult ReadCookies()
{
var cookieVal = Request.Cookies["setting"];
ViewBag.submitOption = cookieVal;
return View("MeoryouViewReturn");
}
Webviews;
<div class="cookieCreator">
<form asp-action="WriteCookies" asp-controller="Meoryou">
<input type="radio" value="Sam" name="submitOption"> Sam<br />
<input type="radio" value="Chris" name="submitOption"> Chris<br />
<input type="radio" value="Other" name="submitOption"> Other<br />
<input type="checkbox" name="isPersistent" value="true" /> Save for 14days?<br />
<div style="color: black;">
<input type="submit" name="submitOption" value="Write Cookie" />
</div><br />
</form>
<h4>#ViewBag.Message</h4>
<h4>
<a class="btn-lg" asp-action="ReadCookies" asp-controller="Meoryou">
See whose turn it is.<br />
</a>
</h4>
Webview Return;
<div>
Turn: #ViewBag.submitOption
</div>

MVC 4 "The using block is missing a } character"

I'm trying to setup a shopping cart for my project.
I am storing the item name, price, and description into Session and call it back and eventually use it for payment processing.
I've written everything and am now having this weird error where it says
"} expected" in the error list.
Also, when I hover over #using in #using (Html.BeginForm("ValidateForm", "PayPal")),
it says "The using block is missing a } character".
I just don't know what the problem is.
Any help would be greatly appreciated.
Thank you.
#{
ViewBag.Title = "ShoppingCart";
}
<h2>ShoppingCart</h2>
#using (Html.BeginForm("ValidateCommand", "PayPal"))
{
var cart = Session["Cart"];
<div class="row">
<div class="col-lg-9">
<h2><strong>Courses</strong></h2><br />
#foreach (var item in cart)
{
<div class="col-md-4 col-xs-6 col-s-8 col-lg-4">
<img src="~/Images/party.gif" style="width: 175px" class="img-responsive" />
<h2>#item.className</h2>
<input type="text" name="product" value="#item.className" hidden="hidden" />
<input type="text" name="totalPrice" value="#item.classPrice" hidden="hidden" />
<input type="text" name="custom" value="#item.ClassID" hidden="hidden" />
}
<br />
</div>
</div>
<input class="btn btn-default" type="submit" name="btnConfirm" value="Check Out with Paypal" />
</div>
}
Your div and /div tags appear to be mis-matched, Razor views care about that sort of thing, make sure you open and close them inside the same code block (defined by curly braces)
You have a "dangling-div". Here:
}
<br />
</div>
That </div> should be inside the foreach brace.

Categories