简体   繁体   English

将数据从视图传递到控制器并返回到同一个视图 ASP Net Core MVC

[英]Passing data from View to Controller and back to the same View ASP Net Core MVC

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"我从日历中选择病人、医生和日期,我想把它发送到控制器,它应该生成可用时间列表,放在 ViewBag.date 的同一页面中,这样我也可以选择时间并通过单击“添加访问”完成添加访问

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"我从日历中选择病人、医生和日期,我想把它发送到控制器,它应该生成可用时间列表,放在 ViewBag.date 的同一页面中,这样我也可以选择时间并通过单击“添加访问”完成添加访问

To achieve the requirement, you can try to define a new property for hour info, like below.为了达到要求,您可以尝试为hour信息定义一个新属性,如下所示。

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更新视图代码以绑定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>

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 :

            <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接下来,我添加了功能“goes”到更改事件到医生列表和日历

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

In goes() function there is code :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);},});}};

where I'm sending selected doctor id and selected date to function "updatedata" in VisitsController, which returns me list of available hours.我在其中发送选定的医生 ID 和选定的日期以在 VisitsController 中运行“updatedata”,这会返回可用时间列表。 I hope someone will find this solution useful.我希望有人会发现这个解决方案很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 asp.net core mvc 中将数据从 Partial View 传递到其父 View - Passing data from Partial View to its parent View in asp.net core mvc ASP.NET MVC Ajax:将IList从视图传递到控制器 - ASP.NET MVC Ajax: Passing an IList from the View to the Controller 通过Ajax调用将数据从asp.net MVC控制器传递到View - Passing data from asp.net MVC controller to the View via Ajax call 在 ASP.NET MVC 中将 HTML 表单数据从视图传递到控制器 - Passing HTML form data from View to Controller in ASP.NET MVC 将数据从MVC控制器返回到同一视图 - Returning data from MVC controller back to same view 成功后,Ajax GET 请求数据无法从 ASP.NET Core MVC 中的控制器查看 - Ajax GET request data is not reaching to view from controller in ASP.NET Core MVC on success 在 C# ASP.NET Core MVC 中使用 AJAX 将数据从视图传递到控制器 - Pass data from view to controller using AJAX in C# ASP.NET Core MVC 将数据从ASP.NET MVC控制器推送到View - Pushing data from an ASP.NET MVC Controller to a View ASP.Net MVC 将表数据从视图发送到 controller - ASP.Net MVC Sending Table data from view to controller ASP .NET Core MVC JQuery AJAX - problem with getting data to view via controller api using ajax - ASP .NET Core MVC JQuery AJAX - problem with getting data to view via controller api using ajax
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM