【问题标题】:how to add and view in same page using mvc如何使用 mvc 在同一页面中添加和查看
【发布时间】:2017-10-26 13:24:34
【问题描述】:

点击按钮后,我们将医生体验保存到数据库中。保存后,我需要使用 foreach 条件在同一页面中查看医生体验详细信息。在我的代码中,foreach 代码不起作用。

 @model MedeilMVC_CLOUD.Models.DoctorMainModel 

    @using (Html.BeginForm("AddExperience", "Doctor", FormMethod.Post, new { id = "myForm", enctype = "multipart/form-data" }))
            {
                @Html.AntiForgeryToken()
                @Html.ValidationSummary(true)


                <a href="#" data-toggle="modal" data-target="#upload3Modal" class="btn btn-rounded btn-block">Add Experience</a>

@foreach (MedeilMVC_CLOUD.Models.DoctorExperience doctor in Model.DoctorExp)
                                { 


                                }

    }

弹出

<div class="modal fade" id="upload3Modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content" style="height: 580px;">
            <div class="modal-header">
                <button type="button" class="modal-close" data-dismiss="modal" aria-label="Close">
                    <i class="font-icon-close-2"></i>
                </button>
                <h4 class="modal-title" id="myModalLabel">Add Experience</h4>
            </div>
            <div class="modal-upload menu-big-icons">
                <div class="modal-upload-cont">
                    <div class="modal-upload-cont-in" style="border-left: none;">
                        <div class="tab-content">
                            <div role="tabpanel" class="tab-pane active" id="tab-upload-3-1">
                                <label class="form-label semibold control-label">Hospital Name</label>
                                @Html.TextBoxFor(model => Model.doctorExperience.HospitalName, new { @class = "form-control", @id = "txtPHospitalName", placeholder = "Enter the Hospital Name" })
                                 @Html.ValidationMessageFor(model => Model.doctorExperience.HospitalName, null, new { @style = "color: red" })    
                            <br />

                                <label class="form-label semibold control-label">Department</label> 
                                @Html.DropDownList("Department", null, "--- Select Department ---", new { @class = "select2-arrow" })
                            @Html.ValidationMessageFor(model => Model.doctorExperience.DepartmentID, null, new { @style = "color: red" })   
                               <br />   <br />
                                <label class="form-label semibold control-label">From Date</label>
                                <div class='input-group date'>
                                    <input id="txtPFromDate" type="text" value="24/10/1984" class="form-control">
                                    <span class="input-group-addon">
                                        <i class="font-icon font-icon-calend"></i>
                                    </span>
                                </div>
                            <br />
                                <label class="form-label semibold control-label">To Date</label>
                                <div class='input-group date'>
                                    <input id="txtPToDate" type="text" value="24/10/1984" class="form-control">
                                    <span class="input-group-addon">
                                        <i class="font-icon font-icon-calend"></i>
                                    </span>
                                </div>
                            <br />
                                <label class="form-label semibold control-label">Work Description</label>
                                @Html.TextAreaFor(model => Model.doctorExperience.WorkDescription, new { @class = "form-control maxlength-simple", @id = "txtPWorkDescription", maxlength = "500", placeholder = "Max length 500 chars" })
                             <br />  
                            <input type="submit" name="submit" id="btnsaveExperience" value="Save Experience" class=" btn btn-rounded btn-inline btn-success" />
                            </div><!--.tab-pane-->
                        </div><!--.tab-content-->
                    </div><!--.modal-upload-cont-in-->
                </div><!--.modal-upload-cont-->
            </div>
        </div>
    </div>
</div><!--.modal-->

类:

public class DoctorMainModel
     {
         public IEnumerable<DoctorExperience> DoctorExp { get; set; }
     }

     [Table("Doctorexperience")]
    public class DoctorExperience
    {
        [Key]
        public int ExperienceID { get; set; }
        public int DoctorID { get; set; }
        [Display(Name = "HospitalName")]
        [Required(ErrorMessage = "Enter the Hospital Name")]
        public string HospitalName { get; set; }
        [Display(Name = "Department")]
        [Required(ErrorMessage = "Enter the Department")]
        public int DepartmentID { get; set; }
        //[Display(Name = "Designation")]
        //[Required(ErrorMessage = "Enter the Designation")]
        //public string Designation { get; set; }
        [Display(Name = "FromDate")]
        [Required(ErrorMessage = "From Date")]
        public DateTime FromDate { get; set; }
        [Display(Name = "FromDate")]
        [Required(ErrorMessage = "To Date")]
        public DateTime ToDate { get; set; }
        public string WorkDescription { get; set; }
        public int CreatedBy = -1;
        public DateTime CreatedDate = DateTime.UtcNow;
        public int ModifiedBy = -1;
        public DateTime ModifiedDate = DateTime.UtcNow;
    }

控制器:

 [HttpPost]
        public ActionResult AddDoctorExperience(DoctorMainModel mainModel)
        {
            try
            {
                int docId = Convert.ToInt32(Session["docID"]);
                if (ModelState.IsValid && docId > 0)
                {
                    DoctorReg obj = new DoctorReg();
                    int id = obj.AddDoctorExperience(mainModel, docId);
                    if (id != 0)
                    {
                        Session["experienceID"] = id.ToString();
                        ViewBag.Message = "Experience added successfully";
                        return Json(new { id = Session["experienceID"] });
                    }
                    return View();
                }
                return View();
            }
            catch
            {
                return View();
            }

        }

【问题讨论】:

标签: c# asp.net asp.net-mvc asp.net-mvc-4 model-view-controller


【解决方案1】:

如果要查看单医生详细信息,需要返回单医生详细信息的同一页面,根据查看详细信息准备视图。否则,如果您要在提交数据后查看医生详细列表,则需要返回医生详细信息列表到同一视图并根据视图列表准备视图。

【讨论】:

  • 例如任何参考代码可用或编辑我的代码?@K Naga Mallesh
【解决方案2】:

当您单击提交按钮时,请求发送到 AddExperience Action 和 Controllar Doctor 您需要获取相同的 Id 数据并将视图传递给对象示例

        public ActionResult AddExperience(modal m)
    {
        //your code for save the current user data in database after add you need to 
        var get = new JobOrderRepository().FirstOrDefault(x => x.Id == m.id);

        return View(get);
    }

【讨论】:

    猜你喜欢
    • 2014-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多