Hi I got no idea how pass simple data from View to Controller which I need to generate values for update the same View, let me show code for better understanding. I want add Visit which contains Patient, Doctor and date of visit (day and hour) so in Create page I got code :
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="PatientId" class="control-label"></label>
<select asp-for="PatientId" class="form-control" asp-items="ViewBag.PatientId"></select>
</div>
<div class="form-group">
<label asp-for="DoctorId" class="control-label"></label>
<select asp-for="DoctorId" id="iddoctor" class="form-control" asp-items="ViewBag.DoctorId" onchange="go()">
</select>
</div>
<div class="form-group">
<label asp-for="date" class="control-label"></label>
<input asp-for="date" class="form-control" />
<span asp-validation-for="date" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="date" class="control-label"></label>
<select asp-for="date" class="form-control" asp-items="ViewBag.date"></select>
</div>
I select patient, doctor and date from calendar and I want send it to Controller which should generate list of avaiable hours to put in the same page in ViewBag.date so I can select also hour and finish adding visit by click "add visit"
My Controller Create function
public IActionResult Create()
{
ViewData["DoctorId"] = new SelectList(_context.Doctors, "DoctorId", "Surname" );
ViewData["PatientId"] = new SelectList(_context.Patients, "PatientId", "Surname" );
return View();
}
EDIT : Solution in my answer below
I select patient, doctor and date from calendar and I want send it to Controller which should generate list of avaiable hours to put in the same page in ViewBag.date so I can select also hour and finish adding visit by click "add visit"
To achieve the requirement, you can try to define a new property for hour
info, like below.
public class VisitViewModel
{
public int PatientId { get; set; }
public int DoctorId { get; set; }
[DataType(DataType.Date)]
public DateTime date { get; set; }
public string hour { get; set; }
}
Update View code to bind hour
field
@model VisitViewModel
@{
ViewData["Title"] = "Create";
}
<h1>Create</h1>
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="PatientId" class="control-label"></label>
<select asp-for="PatientId" class="form-control" asp-items="ViewBag.PatientId"></select>
</div>
<div class="form-group">
<label asp-for="DoctorId" class="control-label"></label>
<select asp-for="DoctorId" id="iddoctor" class="form-control" asp-items="ViewBag.DoctorId" onchange="go()">
</select>
</div>
<div class="form-group">
<label asp-for="date" class="control-label"></label>
<input asp-for="date" class="form-control" />
<span asp-validation-for="date" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="hour" class="control-label"></label>
<select asp-for="hour" class="form-control" asp-items="ViewBag.date"></select>
</div>
<input type="submit" value="Create" />
</form>
In action method, generate new datetime based on selected day and hour.
public IActionResult Create(VisitViewModel visit)
{
var date = visit.date.ToShortDateString();
var newdate = Convert.ToDateTime(date + " " + visit.hour);
//...
//code logic
//...
Test Result
Ok I found solution by myself. I replaced last div in my View for this one :
<div class="form-group">
<label asp-for="hour" class="control-label"></label>
<select asp-for="hour" class="form-control" asp-items="ViewBag.hour">
</select>
</div>
Next I added function "goes" to on change event to Doctor list and calendar
<select asp-for="DoctorId" id="iddoctor" class="form-control" onchange= "goes()" asp-
items="ViewBag.DoctorId">
In goes()
function there is code :
$.ajax({
url: '@Url.Action("Updatedata", "Visits")',
dataType: "json",
data: { x, data: y} ,
success: function (data) {
var items = data;
var item = "";
var itemss
for (var i = 0; i < items.length; i++) {
item = items[i];
item = item.replace("0001-01-01T", "").slice(0, -3);
itemss += "<option value='" + item + "'>" + item + "
</option>"
}
$('#hour').html(itemss);},});}};
where I'm sending selected doctor id and selected date to function "updatedata" in VisitsController, which returns me list of available hours. I hope someone will find this solution useful.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.