如何从数据库MVC预填充TexboxFor

时间:2018-10-11 17:00:36

标签: c# sql asp.net-mvc razor

我在表格中有一个表格,看起来像这样 enter image description here

我要做的是,当用户选择“日期”并单击Search时,应在分钟文本框中填充与每个SID对应的分钟。

我的模特

 public class InsertedMinutes
{
    public string SID { get; set; }

    public string FullName { get; set; }

    public string Minutes { get; set; }

    public string ClassID { get; set; }

    public DateTime Date { get; set; }

}

我的控制器

public class ClassController : Controller
{
    DAL.DB dblayer = new DAL.DB();

    public ActionResult Class(ClassModel ClassModel, string datepicker)
    {
        List<ClassModel> students = dblayer.GetClass(ClassModel.InstructorSID, ClassModel.StartTime);
        var InsSID = ClassModel.InstructorSID;
        var time = ClassModel.StartTime;
        ViewBag.InstructorSID = InsSID;
        ViewBag.StartTime = time;

        return View(students);
    }

    [HttpPost]
    public ActionResult Index(List<ClassModel> students, string datepicker)
    {
        DateTime Date = DateTime.ParseExact(datepicker, "MM/dd/yyyy", null);
        //ClassModel s = new ClassModel();
        //ViewData["InstructorSID"] = s.InstructorSID;

        TryUpdateModel(students);
        if (ModelState.IsValid)
        {
            DB classes = new DB();
            string msg = classes.Minutes(students, Date);
            //TempData["Message"] = msg;
            ViewData["Message"] = "Minutes Inserted";
        }

        return View();
    }

    public InsertedMinutes Update (string datepicker, string ClassID)
    {
        ClassModel s = new ClassModel();
        ClassID = s.ClassID;

        DateTime Date = DateTime.ParseExact(datepicker, "MM/dd/yyyy", null);

        return new DB().Getminutes(Date, ClassID);        
    }        
}

对于我的类InsertedMinutes更新方法,这是应该获取并填充我的文本框的方法,在我的数据库上,我有这种逻辑

 //GET Minutes for Instructor View
    public InsertedMinutes Getminutes(DateTime Date, string ClassID)
    {
        InsertedMinutes s = new InsertedMinutes();
        string connectionString = ConfigurationManager.ConnectionStrings["ABEAttendanceDB"].ConnectionString;
        using (SqlConnection myConnection = new SqlConnection(connectionString))
        {
            SqlCommand myCommand = new SqlCommand("[sp_ABEAttendanceGetMinutes]", myConnection);
            myCommand.CommandType = CommandType.StoredProcedure;
            SqlParameter DateParam = myCommand.Parameters.Add("Date", SqlDbType.Bit);
            DateParam.Value = Date;

            SqlParameter ClassIDParam = myCommand.Parameters.Add("ClassID", SqlDbType.Bit);
            ClassIDParam.Value = ClassID;

            myConnection.Open();
            using (SqlDataReader oReader = myCommand.ExecuteReader())
            {
                while (oReader.Read())
                {                     
                    {
                        s.SID = SafeGetString(oReader, "SID");
                        s.FullName = SafeGetString(oReader, "FullName");
                        s.Minutes = SafeGetString(oReader, "Minutes");
                        s.Date = SafeGetDate(oReader, "Date");
                        s.ClassID = SafeGetString(oReader, "ClassID");

                    };                       
                }
                myConnection.Close();
            }
        }

        return s;
    }

这是我的观点:

@model List<ABEAttendanceV3.Models.ClassModel>

@{
ViewBag.Title = "Class";
}

<script>
var date = new Date();
var maxdate = date.getDate();
$(function () {
    $("#datepicker").datepicker({
        defaultDate: maxdate,
        minDate: '-1M',
        maxDate: '+0D'
    });
   });

 </script>

 <div>
@using (Html.BeginForm("Class", "Class", new { InstructorSID = 
ViewBag.InstructorSID, StartTime = ViewBag.StartTime }))
{

    @Html.ValidationSummary(true)
    <fieldset>
        <div>
            <table id="tablereport1" class="table table-striped table- 
     bordered table-hover table-condensed">
                <thead style="background-color:black; font-weight:bold; 
     color:aliceblue">
                    <tr>
                </thead>
                <tr>
                    <td><b>@Html.Label("Date :")</b></td>

                    <td>
                        @Html.TextBox("datepicker", 
       DateTime.Now.ToString("MM/dd/yyyy"), new { required = "required" }) 
      <b>  <input type="hidden" value="Search" id="btn-search" class="btn 
       btn-default" /></b>
           <span>
            @Html.ActionLink("Search", "Update", "Class", new { @class = 
     "btn btn- success btn-md searchBtn", StartTime = DateTime.Now })
                                            </span>
                    </td>

                    <td align="right">
                        <b>@Html.Label("Select Minutes :")</b>

                        <select id="dropdown">
                            <option value="50">50</option>
                            <option value="110">110</option>
                        </select>
                    </td>

                </tr>

                @*<tr>
                        <td><input type="button" value="Add 50 Minutes" 
              id="add-50" class="btn btn-default" /></td>
                    </tr>*@
            </table>
            @if (ViewBag.Message != null)
            {
                <span class="success sweet-alert"> @ViewBag.Message</span>
            }

            @*<button class="textbox">A button element</button>*@

        </div>
        @*@if (ViewBag.Message != null)
            {
                <div id="dialog-message" title="Alert !!">
                    <p>
                        <span class="ui-icon ui-icon-circle-check" 
                 style="float:left; margin:0 7px 50px 0;"></span>
                        @ViewBag.Message
                    </p>
                </div>
            }*@

        <table id="tablereport2" class="table table-striped table-bordered 
           table-hover table-condensed">
            <thead style="background-color:black; font-weight:bold; 
                color:aliceblue">
                <tr>

            </thead>
            <tr>
                <th>Class ID</th>
                <th>Course Title</th>
                <th>Department</th>
                <th>SID</th>
                <th>Full Name</th>
                <th>Minutes</th>
            </tr>

            @for (var i = 0; i < Model.Count; i++)
            {
                <tr id="texbox">
                    <td>
                        @Html.DisplayFor(model => model[i].ClassID)
                        @Html.HiddenFor(model => model[i].ClassID)
                    </td>
                    <td>
                        @Html.DisplayFor(model => model[i].CourseTitle)
                        @Html.HiddenFor(model => model[i].CourseTitle)
                    </td>
                    <td>
                        @Html.DisplayFor(model => model[i].Department)
                        @Html.HiddenFor(model => model[i].Department)
                    </td>
                    <td>
                        @Html.DisplayFor(model => model[i].SID)
                        @Html.HiddenFor(model => model[i].SID)
                    </td>
                    <td>
                        @Html.DisplayFor(model => model[i].FullName)
                        @Html.HiddenFor(model => model[i].FullName)
                    </td>

                    <td>
                        @Html.TextBoxFor(model => model[i].Minutes, new { 
                  placeholder = "0", @class = "minutes" })
                        @Html.ValidationMessageFor(model => 
                       model[i].Minutes)
                    </td>

                </tr>
            }
        </table>
        <table align="center">
            <tr>
                <td>
                    <input type="hidden" value="Save" id="btn-submit" 
                        class="btn btn-default" />
                    <span>
                        @Html.ActionLink("Save", "Class", "Class",
                      new { @class = "btn btn-success btn-md insertBtn", 
                                          StartTime = DateTime.Now })
                    </span>
                </td>
            </tr>
        </table>
    </fieldset>
     }

          </div>

  <script>
    $(function () {

    $(".insertBtn").click(function (e) {
        //whenever our button is clicked

        e.preventDefault();
        // stop the default behavior(navigation)
        var _form = $(this).closest("form");
        //get a reference to the container form

        swal({
            title: "Good!",
            text: "Minutes Has Been Inserted!",
            icon: "success",
            button: "Ok!",
        }).then(function () {
            //user selected "Yes", Let's submit the form
            _form.submit();
        });

    });

})
 </script>
 <script>

$(function () {
    $('#dropdown').change(function () {
        var minutes = $(this).val();
        if (minutes != "")
            $('.minutes').val(minutes);
    });
});

  </script>

以你们的经验,什么是最好的方法?

0 个答案:

没有答案