Populating field on Razor view - asp.net-core

I'm actually new in asp.net core and don't know much about it
My question is.
I want to populate a text field on Razor view when an enum item is selected from dropdown list but have no idea about how to do it
I have searched about this but didn't find any thing
I want to populate Price field when item is selected from FightClass
View
#model Airline.Models.BookingModel
#{
ViewData["Title"] = "Buy";
}
<h2>Buy</h2>
<h4>BookingModel</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Buy">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input asp-for="FlightSkyMiles" type="hidden" value="#ViewBag.Flight.SkyMiles" />
<div class="form-group">
<label asp-for="FlightName" class="control-label"></label>
<input asp-for="FlightName" value="#ViewBag.Flight.FlightName" class="form-control" readonly />
<span asp-validation-for="FlightName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="FlightNo" class="control-label"></label>
<input asp-for="FlightNo" value="#ViewBag.Flight.FlightNo" class="form-control" readonly />
<span asp-validation-for="FlightNo" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="OriginCity" class="control-label"></label>
<input asp-for="OriginCity" value="#ViewBag.Flight.OriginCity" class="form-control" readonly />
<span asp-validation-for="OriginCity" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="DestinationCity" class="control-label"></label>
<input asp-for="DestinationCity" value="#ViewBag.Flight.DestinationCity" class="form-control" readonly />
<span asp-validation-for="DestinationCity" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Departure" class="control-label"></label>
<input asp-for="Departure" type="datetime" value="#ViewBag.Flight.Departure" class="form-control" readonly />
<span asp-validation-for="Departure" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Adult" class="control-label"></label>
<input asp-for="Adult" type="text" name="seats" value="0" class="form-control" />
<span asp-validation-for="Adult" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Children" class="control-label"></label>
<input asp-for="Children" type="text" name="seats" value="0" class="form-control" />
<span asp-validation-for="Children" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Senior" class="control-label"></label>
<input asp-for="Senior" type="text" name="seats" value="0" class="form-control" />
<span asp-validation-for="Senior" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Travelers" class="control-label"></label>
<input asp-for="Travelers" type="text" name="TotalSeats" class="form-control" />
<span asp-validation-for="Travelers" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="FlightClass" class="control-label"></label>
<select class="form-control" asp-for="FlightClass" name="fclass" asp-items="Html.GetEnumSelectList<Classtype>()">
<option selected="selected" value="">Please select</option>
</select>
<span asp-validation-for="FlightClass" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Price" class="control-label"></label>
<input asp-for="Price" class="form-control" readonly />
<span asp-validation-for="Price" class="text-danger"></span>
</div>
<div class="form-group">
<input type="radio" name="Reservation" value="Buy" /> Buy
<input type="radio" name="Reservation" value="Block" /> Block
<span asp-validation-for="ReservationType" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Submit" class="btn btn-default" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
If anyone can help me I will be very thankful

You can set onchange event to select :
<select onchange="getPrice()" class="form-control" asp-for="FlightClass" name="fclass" asp-items="Html.GetEnumSelectList<Classtype>()">
<option selected="selected" value="">Please select</option>
</select>
So that when select changed , you can make ajax call to server side to get price based on Classtype:
<script>
function getPrice() {
$.ajax
({
url: '/home/getPrice',
type: 'post',
data: { 'id' : $("#FlightClass").val() },
success: function (result) {
$("#Price").val(result.price);
},
error: function () {
alert("Error")
}
});
}
</script>
On controller side , you can query the price by id :
public IActionResult getPrice(string id)
{
//get price by id
var price = "";
return new JsonResult(new { price = price });
}

Related

I want to show two different alerts on one method and one form

I have only one form for update and create and one button in this form. I want when I will click on submit button then alert will must show automatically update or create that alert which i will get request through controller. I am not able to use data-ajax call. So tell me about how I will show-ajax call. or give me a sample of this and this is my code and help me about this.
<form id="blogForm" enctype="multipart/form-data" asp-controller="Admin" asp-action="AddBlog" method="post">
<div class="card-body">
<div class="form-group">
<input type="hidden" asp-for="blogRequestDTO.Id" />
</div>
<div class="form-group">
<label asp-for="blogRequestDTO.Title">Title</label>
<input type="text" asp-for="blogRequestDTO.Title" class="form-control" placeholder="Enter blog name">
<span asp-validation-for="blogRequestDTO.Title" class="text-danger"></span>
</div>
<!-- For Image-->
<div class="col-md-6">
<div class="form-group">
<label asp-for="blogRequestDTO.Image" class="control-label"></label>
<div class="custom-file">
<input asp-for="blogRequestDTO.Image" class="custom-file-input" id="customFile">
<label class="custom-file-label" for="customFile">Choose file</label>
</div>
<span asp-validation-for="blogRequestDTO.Image" class="text-danger"></span>
</div>
</div>
</div>
<!-- /.card-body -->
<div class="card-footer">
<button id="submitBtn" type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
Maybe what you want is to use onclick to call ajax when submitting.
Below is my test code you can refer to it.
#model Project.Models.blogRequestDTO
<form id="blogForm" enctype="multipart/form-data" asp-controller="Home" asp-action="AddBlog" method="post">
<div class="card-body">
<div class="form-group">
<input type="hidden" id="Id" asp-for="#Model.Id" />
</div>
<div class="form-group">
<label asp-for="#Model.Title">Title</label>
<input type="text" id="Title" asp-for="#Model.Title" class="form-control" placeholder="Enter blog name">
<span asp-validation-for="#Model.Title" class="text-danger"></span>
</div>
<!-- For Image-->
<div class="col-md-6">
<div class="form-group">
<label asp-for="#Model.Image" class="control-label"></label>
<div class="custom-file">
<input id="Image" asp-for="#Model.Image" class="custom-file-input" id="customFile">
<label class="custom-file-label" for="customFile">Choose file</label>
</div>
<span asp-validation-for="#Model.Image" class="text-danger"></span>
</div>
</div>
</div>
<!-- /.card-body -->
<div class="card-footer">
<button id="submitBtn" class="btn btn-primary" onclick="Test()" type="submit" >submit</button>
</div>
</form>
<script>
function Test()
{
var formData = new FormData();
formData.append("Id", $("#Id").val());
formData.append("Title",$("#Title").val());
formData.append("Image",$("#Image")[0].files[0]);
debugger;
$.ajax({
url: '/Home/AddBlog',
method: 'POST',
contentType: 'json',
data: formData,
processData: false,
contentType: false,
cache: false,
success: function (response) {
alert('success');
},
error: function (response, error) {
alert('error');
}
});
}
</script>

Assigning a value by checking a box in razor pages

I`m making a web app for online exams and after i write the question and the answers, i want to select the right answer by checking a checkbox. [The right answer][1] will have the same value as the checked answer but i do not know how to make it appear in the [question list][2] or in the [database][3].
Model variables:
public string question{get;set;}
public string asnwer1{get;set;}
public string answer2{get;set;}
public string answer3{get;set;}
public string answer4{get;set;}
public string rightanswer{get;set;}
Create question page cshtml:
Intrebare = Question | Optiune = Answer | Raspuns = Right Answer.
<div class="form-group">
<label asp-for="AplicatieIntrebare.Intrebare" class="control-label"></label>
<input asp-for="AplicatieIntrebare.Intrebare" class="form-control" />
<span asp-validation-for="AplicatieIntrebare.Intrebare" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="AplicatieIntrebare.Optiune1" class="control-label"></label>
<input asp-for="AplicatieIntrebare.Optiune1" class="form-control" />
<span asp-validation-for="AplicatieIntrebare.Optiune1" class="text-danger"></span>
<input id="Checkbox1" type="checkbox" />
</div>
<div class="form-group">
<label asp-for="AplicatieIntrebare.Optiune2" class="control-label"></label>
<input asp-for="AplicatieIntrebare.Optiune2" class="form-control" />
<span asp-validation-for="AplicatieIntrebare.Optiune2" class="text-danger"></span>
<input id="Checkbox2" type="checkbox" />
</div>
<div class="form-group">
<label asp-for="AplicatieIntrebare.Optiune3" class="control-label"></label>
<input asp-for="AplicatieIntrebare.Optiune3" class="form-control" />
<span asp-validation-for="AplicatieIntrebare.Optiune3" class="text-danger"></span>
<input id="Checkbox3" type="checkbox" />
</div>
<div class="form-group">
<label asp-for="AplicatieIntrebare.Optiune4" class="control-label"></label>
<input asp-for="AplicatieIntrebare.Optiune4" class="form-control" />
<span asp-validation-for="AplicatieIntrebare.Optiune4" class="text-danger"></span>
<input id="Checkbox4" type="checkbox" />
</div>
Thank you!
[1]:https://i.stack.imgur.com/MRUqr.png
[2]:https://i.stack.imgur.com/sgnst.png
[3]:https://i.stack.imgur.com/ztIIc.png
If you want to bind Raspuns when the checkbox is checked,you can add a hidden input with asp-for="AplicatieIntrebare.Raspuns",and before posting form,iterate div with class="form-group",then check if the check box in the div is checked.If so,set the hidden input value with the input's value in the div.Here is a demo:
cshtml:
<form method="post" id="myform">
<div class="form-group">
<label asp-for="AplicatieIntrebare.Intrebare" class="control-label"></label>
<input asp-for="AplicatieIntrebare.Intrebare" class="form-control" />
<span asp-validation-for="AplicatieIntrebare.Intrebare" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="AplicatieIntrebare.Optiune1" class="control-label"></label>
<input asp-for="AplicatieIntrebare.Optiune1" class="form-control" />
<span asp-validation-for="AplicatieIntrebare.Optiune1" class="text-danger"></span>
<input id="Checkbox1" type="checkbox" />
</div>
<div class="form-group">
<label asp-for="AplicatieIntrebare.Optiune2" class="control-label"></label>
<input asp-for="AplicatieIntrebare.Optiune2" class="form-control" />
<span asp-validation-for="AplicatieIntrebare.Optiune2" class="text-danger"></span>
<input id="Checkbox2" type="checkbox" />
</div>
<div class="form-group">
<label asp-for="AplicatieIntrebare.Optiune3" class="control-label"></label>
<input asp-for="AplicatieIntrebare.Optiune3" class="form-control" />
<span asp-validation-for="AplicatieIntrebare.Optiune3" class="text-danger"></span>
<input id="Checkbox3" type="checkbox" />
</div>
<div class="form-group">
<label asp-for="AplicatieIntrebare.Optiune4" class="control-label"></label>
<input asp-for="AplicatieIntrebare.Optiune4" class="form-control" />
<span asp-validation-for="AplicatieIntrebare.Optiune4" class="text-danger"></span>
<input id="Checkbox4" type="checkbox" />
</div>
<input type="submit" value="submit" />
<input asp-for="AplicatieIntrebare.Raspuns" hidden />
</form>
#section scripts {
<script>
$("#myform").submit(function () {
$(".form-group").each(function (index, element) {
if ($(element).children("input[type='checkbox']").prop("checked")) {
$("#AplicatieIntrebare_Raspuns").attr("value", $(element).children("input[type='text']").val());
}
})
})
</script>
}
cshtml.cs:
[BindProperty]
public AplicatieIntrebare AplicatieIntrebare { get; set; }
public void OnGet()
{
AplicatieIntrebare = new AplicatieIntrebare { Intrebare = "What's my dog's name?", Optiune1 = "Jerry", Optiune2 = "Spot", Optiune3 = "Rex", Optiune4 = "Max" };
}
public void OnPost()
{
}
result:

How to hide some textfields in razor view in asp.net core

I'm implementing a project by asp.net core. I have a selectlist item which its options are loaded from a model and it has 2 items. My problem is, I want in the razor view, according to the selected option in selectlist, some TextFields be shown to the user. For example if the user choose the first option, three of the fields be shown to the user and if the user selects the second option, other fields be shown to the user meanwhile the other three field be hidden. For doing this. I have tried some code like below in my Create view:
#model CSDDashboard.Models.ApplicantViewModel
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"> </script>
<script>
$(document).ready(function () {
$('#applicantvm.ApplicantType').change(function () {
var value = $(this).val();
if (value == '1') {
$(".legal").hide();
} else {
$(".person").show();
}
});
});
</script>
#{
ViewData["Title"] = "Create";
}
<h1>Create</h1>
<h4>Applicant</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="applicantvm.ApplicantType" class="control-label"></label>
<select asp-for="applicantvm.ApplicantType" class="form-control" asp-items="ViewBag.ApplicantType"></select>
</div>
<div class="form-group">
<label asp-for="applicantvm.Address" class="control-label"></label>
<input asp-for="applicantvm.Address" class="form-control" />
<span asp-validation-for="applicantvm.Address" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="applicantvm.Description" class="control-label"></label>
<input asp-for="applicantvm.Description" class="form-control" />
<span asp-validation-for="applicantvm.Description" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="applicantvm.Name" class="control-label"></label>
<input asp-for="applicantvm.Name" class="form-control" />
<span asp-validation-for="applicantvm.Name" class="text-danger"></span>
</div>
<div class="form-group legal">
<label asp-for="legalapplicantvm.EconomicCode" class="control-label"></label>
<input asp-for="legalapplicantvm.EconomicCode" class="form-control" />
<span asp-validation-for="legalapplicantvm.EconomicCode" class="text-danger"></span>
</div>
<div class="form-group legal">
<label asp-for="legalapplicantvm.NationalCode" class="control-label"></label>
<input asp-for="legalapplicantvm.NationalCode" class="form-control" />
<span asp-validation-for="legalapplicantvm.NationalCode" class="text-danger"></span>
</div>
<div class="form-group legal">
<label asp-for="legalapplicantvm.RegisterNo" class="control-label"></label>
<input asp-for="legalapplicantvm.RegisterNo" class="form-control" />
<span asp-validation-for="legalapplicantvm.RegisterNo" class="text-danger"></span>
</div>
<div class="form-group person">
<label asp-for="personapplicantvm.BirthCertificateNo" class="control-label"></label>
<input asp-for="personapplicantvm.BirthCertificateNo" class="form-control" />
<span asp-validation-for="personapplicantvm.BirthCertificateNo" class="text-danger"></span>
</div>
<div class="form-group person">
<label asp-for="personapplicantvm.IssuePlace" class="control-label"></label>
<input asp-for="personapplicantvm.IssuePlace" class="form-control"/>
<span asp-validation-for="personapplicantvm.IssuePlace" class="text-danger"></span>
</div>
<div class="form-group person">
<label asp-for="personapplicantvm.NationalCode" class="control-label"></label>
<input asp-for="personapplicantvm.NationalCode" class="form-control" />
<span asp-validation-for="personapplicantvm.NationalCode" class="text-danger"></span>
</div>
<div class="form-group person">
<label asp-for="personapplicantvm.Username" class="control-label"></label>
<input asp-for="personapplicantvm.Username" class="form-control" />
<span asp-validation-for="personapplicantvm.Username" class="text-danger"></span>
</div>
}
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back To List</a>
</div>
#section Scripts {
#{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
After running the project, the view is the same as before and all the fields will be visible. I appreciate if anyone suggest me a solution.
asp-for="applicantvm.ApplicantType" render the id and name in the html like id="applicantvm_ApplicantType" name="applicantvm.ApplicantType" ,you could use the developer tools to check the Source code.
So you should change your javascript as shown:
<script>
$(document).ready(function () {
$(".person").hide();
$(".legal").hide();
$('#applicantvm_ApplicantType').change(function () {
var value = $(this).val();
if (value == '1') {
$(".legal").show();
if ($(".person").show()) {
$(".person").hide();
}
} else {
$(".person").show();
if ($(".legal").show()) {
$(".legal").hide();
}
}
});
});
</script>
Result:
In Simple terms,
Write ONCHANGE Event in the drop down and invoke a JavaScript function.
From JavaScript, based on the drop down values, show and hide the controls.

.Net Core Razor Pages - Partial View with multiple objects

I’m pretty new to .Net Core, EF Core & Razor pages.
I have a form which uses posts to a razor page. The page model has three bound properties, two of which are EF Models (I use inheritance to only bind safe properties) and one is just a string:
[BindProperty]
public Models.BaseEvent _Event { get; set; }
[BindProperty]
[Display(Name = "Venue Search")]
public string _VenueSearch { get; set; }
[BindProperty]
public Models.BaseVenue _Venue { get; set; }
I then have a form, used to create an event. When adding information about the event, they can either choose an existing venue, using the venue search, or add a new one. _VenueSearch is bound only to automatically re-populate the form if there is an error. I use jquery to perform autocomplete on the field.
However, I’m trying to put the form into a partial, but I have no idea how to pass in _Event, _VenueSearch & _Venue?
<form method="post">
<input id="venueid" type="hidden" asp-for="_Venue.VenueID" />
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="_Event.Name" class="control-label"></label>
<input asp-for="_Event.Name" class="form-control" />
<span asp-validation-for="_Event.Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="_Event.StartDateTime" class="control-label"></label>
<input asp-for="_Event.StartDateTime" class="form-control" />
<span asp-validation-for="_Event.StartDateTime" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="_Event.EndDateTime" class="control-label"></label>
<input asp-for="_Event.EndDateTime" class="form-control" />
<span asp-validation-for="_Event.EndDateTime" class="text-danger"></span>
</div>
<hr />
<div id="search-container" class="form-group">
<label asp-for="_VenueSearch" class="control-label"></label>
<input id="venue" asp-for="_VenueSearch" class="form-control" />
<span asp-validation-for="_VenueSearch" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="_Venue.Name" class="control-label">Venue Name</label>
<input id="venue-name" asp-for="_Venue.Name" class="form-control" />
<span asp-validation-for="_Venue.Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="_Venue.Address" class="control-label">Venue Address</label>
<input id="venue-address" asp-for="_Venue.Address" class="form-control" />
<span asp-validation-for="_Venue.Address" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="_Venue.City" class="control-label">Venue City</label>
<input id="venue-city" asp-for="_Venue.City" class="form-control" />
<span asp-validation-for="_Venue.City" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="_Venue.PostCode" class="control-label">Venue Postcode</label>
<input id="venue-postcode" asp-for="_Venue.PostCode" class="form-control" />
<span asp-validation-for="_Venue.PostCode" class="text-danger"></span>
</div>
<hr />
<div class="form-group">
<label asp-for="_Event.Description" class="control-label"></label>
<textarea asp-for="_Event.Description" class="form-control"></textarea>
<span asp-validation-for="_Event.Description" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="_Event.AgeRestriction" class="control-label"></label>
<input asp-for="_Event.AgeRestriction" class="form-control" />
<span asp-validation-for="_Event.AgeRestriction" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create Event" class="btn btn-primary" />
</div>
</form>
How would I put this in a partial so that I still have access to the two models and string (_Event, _Venue & _VenueSearch. I haven’t included the javascript autocomplete code as its irrelevant to this post.
I want to use a partial as both my create/edit pages will use the same form, so I don’t see the point of duplication.
Thanks in advance for any help
Matt
You can create a view model and includes the objects which query from database , when loading the partial view ,directly pass the view model to partial view :
#await Html.PartialAsync("viewName", model:Model.YourModel)

multi-step registration using bootstrapvalidator-prevent proceeding on error

I have a multi-step reg form with each step in a fieldset,i am using bootstrap validator for validation.I made the 'next' button in first step to toggle validation.And it works well...
The problem i have is,i have to prevent going to next step on an error.
Now, validation works on clicking 'next'in first step but immediately passes to next step.
I want to stay there until validation is success...
How to do this?
i m giving my chunk of code...i have all libraries included...
...
<fieldset id="first_step">
<h2 class="fs-title">Create your account</h2>
<div class="form-group">
<label class="col-md-3 control-label">Username</label>
<div class="col-md-9">
<input type="text" class="form-control" name="uname" />
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Password</label>
<div class="col-md-9">
<input type="password" class="form-control" name="pwd" />
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Confirm Password</label>
<div class="col-md-9">
<input type="password" class="form-control" id="cpwd" />
</div>
</div>
<input type="button" name="next" id="next" class="next action-button" value="Next" />
</fieldset>
<fieldset>
<h2 class="fs-title">Personal Details</h2>
<h3 class="fs-subtitle"></h3>
<div class="form-group">
<label class="col-md-3 control-label">*Register Number</label>
<div class="col-md-9">
<input type="text" class="form-control" id="Text1" name="reg" />
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">*Name</label>
<div class="col-md-9">
<input type="text" class="form-control" id="Text2" name="stname" />
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label"></label>
<div class="col-md-offset-3 col-md-12">
</div>
</div>
<input type="button" name="previous" class="previous action-button" value="Previous" />
<input type="submit" id="sub_but" name="submit" class="submit action-button" value="Submit" />
</fieldset>
<script type="text/javascript">
$(".next").click(function() {
$('#msform').data('bootstrapValidator').validate();
//prevent here????
//Script to slide first fielset
}
$(".previous").click(function() {
//Script to slide back to first fieldset
}
</script>