首页 > 解决方案 > 将数据从 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();
    }

编辑:下面我的答案中的解决方案

标签: ajaxasp.net-core-mvc

解决方案


我从日历中选择患者、医生和日期,并将其发送到控制器,控制器应生成可用时间列表以放入 ViewBag.date 中的同一页面,这样我也可以选择时间并通过单击“添加访问”完成添加访问

要达到要求,您可以尝试为hourinfo 定义一个新属性,如下所示。

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

    //...

测试结果

在此处输入图像描述


推荐阅读