ajax - 将数据从 View 传递到 Controller 并返回到同一个 View ASP Net Core MVC
问题描述
嗨,我不知道如何将简单数据从 View 传递到 Controller,我需要生成更新相同 View 的值,让我展示代码以便更好地理解。我想添加包含患者、医生和访问日期(日期和时间)的访问,所以在创建页面中我得到了代码:
<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>
我从日历中选择患者、医生和日期,并将其发送到控制器,控制器应生成可用时间列表以放入 ViewBag.date 中的同一页面,这样我也可以选择时间并通过单击“添加访问”完成添加访问
我的控制器创建功能
public IActionResult Create()
{
ViewData["DoctorId"] = new SelectList(_context.Doctors, "DoctorId", "Surname" );
ViewData["PatientId"] = new SelectList(_context.Patients, "PatientId", "Surname" );
return View();
}
编辑:下面我的答案中的解决方案
解决方案
我从日历中选择患者、医生和日期,并将其发送到控制器,控制器应生成可用时间列表以放入 ViewBag.date 中的同一页面,这样我也可以选择时间并通过单击“添加访问”完成添加访问
要达到要求,您可以尝试为hour
info 定义一个新属性,如下所示。
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; }
}
更新视图代码以绑定hour
字段
@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>
在 action 方法中,根据选定的日期和时间生成新的日期时间。
public IActionResult Create(VisitViewModel visit)
{
var date = visit.date.ToShortDateString();
var newdate = Convert.ToDateTime(date + " " + visit.hour);
//...
//code logic
//...
测试结果
推荐阅读
- javascript - 如何为动态创建的导航选项卡添加价值?
- php - PHP Google Drive Api v3 - 连接问题(快速入门)
- wordpress - 如果未登录,如何重定向 wordpress 用户
- python - ValueError: max_features must be in (0, n_features] in Random Forest
- python - 从数据帧中删除不连续的时间序列 - 速度提高?
- pandas - 熊猫 to_sql 与 Exasol
- apache - Xampp:Apache 无法启动(控制面板 v3.2.4)
- javascript - 如何以编程方式单击按钮并使用 jQuery 传递参数?
- excel - Firebase:从firebase实时同步数据到excel
- flutter - Flutter 导航到其他路由后返回黑屏