【问题标题】:Passing data from View to Controller and back to the same View ASP Net Core MVC将数据从 View 传递到 Controller 并返回到同一个 View ASP Net Core MVC
【发布时间】:2020-11-20 22:42:13
【问题描述】:

您好,我不知道如何将简单数据从 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();
    }

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

【问题讨论】:

    标签: ajax asp.net-core-mvc


    【解决方案1】:

    我从日历中选择患者、医生和日期,并将其发送到控制器,控制器应生成可用时间列表以放入 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
    
        //...
    

    测试结果

    【讨论】:

    • 我不知道您是否理解我的意思,或者我不明白您的回复。在我点击“创建”按钮并将我的访问添加到数据库之前,我希望在选择合适的医生后查看“刷新”小时列表 - 在我不选择医生之前,我不知道有多少小时可用。跨度>
    • I want "refresh" hours list in view after choosing a right doctor - I don't know what hours are available before I don't choose my doctor.您想根据所选医生和日期为可用时间实施级联下拉列表吗?
    • 是的,正是:-)
    【解决方案2】:

    好的,我自己找到了解决方案。我将视图中的最后一个 div 替换为这个:

                <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> 
    

    接下来我添加了功能“去”到医生列表和日历的更改事件

     <select asp-for="DoctorId" id="iddoctor" class="form-control" onchange= "goes()" asp- 
     items="ViewBag.DoctorId">
    

    goes() 函数中有代码:

     $.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);},});}};
    

    我将选定的医生 ID 和选定的日期发送到 VisitsController 中的“updatedata”函数,它会返回可用时间列表。我希望有人会发现这个解决方案很有用。

    【讨论】:

      猜你喜欢
      • 2019-06-15
      • 2020-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多