locked
CRUD Operations In MVC Using Jquery Ajax RRS feed

  • Question

  • User272872725 posted

    Hi i have problem when using ajax to create asp.net MVC in Model it doesn't working, 

    my code C# controller

    DBQLKHOADataContext data = new DBQLKHOADataContext();
    // GET: SinhVien
    public ActionResult Index()
    {
    return View(data.SINHVIENs.ToList().OrderBy(e => e.MSSV));
    }
    [HttpGet]
    public ActionResult Create()
    {
    ViewBag.ID_LOP = new SelectList(data.SINHVIENs.ToList().OrderBy(n => n.ID_LOP), "ID_LOP", "TENLOP");
    return PartialView("Create");
    }
    [HttpPost]
    public ActionResult Create(FormCollection collection, SINHVIEN sinhvien)
    {
    data.SINHVIENs.InsertOnSubmit(sinhvien);
    data.SubmitChanges();
    ViewBag.ID_LOP = new SelectList(data.SINHVIENs.ToList().OrderBy(n => n.ID_LOP), "ID_LOP", "TENLOP");
    return RedirectToAction("Index", "Sinh");
    }


    code partialView

    @model QLKhoa.Models.SINHVIEN
    @{
    Layout = null;
    }
    <div class="modal" id="myModal">
    <div class="modal-dialog">
    <div class="modal-content">

    <!-- Modal Header -->
    <div class="modal-header">
    <h4 class="modal-title">Modal Heading</h4>
    <button type="button" class="close" data-dismiss="modal">&times;</button>
    </div>

    <!-- Modal body -->
    <div class="modal-body">
    @using (Html.BeginForm())
    {
    @Html.AntiForgeryToken()

    <div class="form-horizontal">

    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
    @Html.LabelFor(model => model.MSSV, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
    @Html.EditorFor(model => model.MSSV, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.MSSV, "", new { @class = "text-danger" })
    </div>
    </div>

    <div class="form-group">
    @Html.LabelFor(model => model.ID_KHOA, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
    @Html.EditorFor(model => model.ID_KHOA, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.ID_KHOA, "", new { @class = "text-danger" })
    </div>
    </div>

    <div class="form-group">
    @Html.LabelFor(model => model.ID_NGANH, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
    @Html.EditorFor(model => model.ID_NGANH, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.ID_NGANH, "", new { @class = "text-danger" })
    </div>
    </div>

    <div class="form-group">
    @Html.LabelFor(model => model.ID_CHUYENNGANH, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
    @Html.EditorFor(model => model.ID_CHUYENNGANH, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.ID_CHUYENNGANH, "", new { @class = "text-danger" })
    </div>
    </div>

    <div class="form-group">
    @Html.LabelFor(model => model.TEN_SV, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
    @Html.EditorFor(model => model.TEN_SV, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.TEN_SV, "", new { @class = "text-danger" })
    </div>
    </div>

    <div class="form-group">
    @Html.LabelFor(model => model.GIOITINH, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
    <div class="checkbox">
    @Html.EditorFor(model => model.GIOITINH)
    @Html.ValidationMessageFor(model => model.GIOITINH, "", new { @class = "text-danger" })
    </div>
    </div>
    </div>

    <div class="form-group">
    @Html.LabelFor(model => model.NGAYSINH, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
    @Html.EditorFor(model => model.NGAYSINH, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.NGAYSINH, "", new { @class = "text-danger" })
    </div>
    </div>

    <div class="form-group">
    @Html.LabelFor(model => model.NGAYBD, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
    @Html.EditorFor(model => model.NGAYBD, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.NGAYBD, "", new { @class = "text-danger" })
    </div>
    </div>

    <div class="form-group">
    @Html.LabelFor(model => model.NGAYKT, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
    @Html.EditorFor(model => model.NGAYKT, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.NGAYKT, "", new { @class = "text-danger" })
    </div>
    </div>

    <div class="form-group">
    @Html.LabelFor(model => model.DOANVIEN, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
    <div class="checkbox">
    @Html.EditorFor(model => model.DOANVIEN)
    @Html.ValidationMessageFor(model => model.DOANVIEN, "", new { @class = "text-danger" })
    </div>
    </div>
    </div>

    <div class="form-group">
    @Html.LabelFor(model => model.DIACHI, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
    @Html.EditorFor(model => model.DIACHI, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.DIACHI, "", new { @class = "text-danger" })
    </div>
    </div>

    <div class="form-group">
    @Html.LabelFor(model => model.SDT, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
    @Html.EditorFor(model => model.SDT, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.SDT, "", new { @class = "text-danger" })
    </div>
    </div>

    <div class="form-group">
    @Html.LabelFor(model => model.EMAIL, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
    @Html.EditorFor(model => model.EMAIL, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.EMAIL, "", new { @class = "text-danger" })
    </div>
    </div>

    <div class="form-group">
    @Html.LabelFor(model => model.ID_LOP, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
    @Html.EditorFor(model => model.ID_LOP, new { htmlAttributes = new { @class = "form-control" } })
    @Html.ValidationMessageFor(model => model.ID_LOP, "", new { @class = "text-danger" })
    </div>
    </div>

    <div class="form-group">
    <div class="col-md-offset-2 col-md-10">
    <input type="submit" value="Create" class="btn btn-default" />
    </div>
    </div>
    </div>
    }
    </div>

    <!-- Modal footer -->
    <div class="modal-footer">
    <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
    </div>

    </div>
    </div>
    </div>
    <div>
    @Html.ActionLink("Back to List", "Index")
    </div>


    code index.cshtml


    <div class="gsrt tw-ta-container tw-nfl" id="tw-target-text-container">

    @model IEnumerable<QLKhoa.Models.SINHVIEN>

    @{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_LayoutAdmin.cshtml";
    }

    <style>
    a {
    color: black;
    }
    </style>
    <h1 style="color:red">Danh Sách Sinh Viên</h1>

    <p>
    <button class="btn btn-primary" id="myModal" data-url="@Url.Action("Create","SinhVien")">Thêm</button>
    </p>
    <table class="table table-striped tab-content" border="1">
    <tr>
    <th style="width:140px;text-align:center">
    Mã Sinh Viên
    </th>
    <th style="width:140px;text-align:center">
    Tên Sinh Viên
    </th>
    <th style="width:140px;text-align:center">
    Ngày Sinh
    </th>
    <th style="width:140px;text-align:center">
    Giới Tính
    </th>
    <th style="width:140px;text-align:center">
    Mã Lớp
    </th>
    <th style="width:140px;text-align:center">
    Khoa
    </th>
    <th style="width:140px;text-align:center">
    Chức Năng
    </th>
    </tr>

    @foreach (var item in Model)
    {
    <tr>
    <td style="text-align:center">
    @Html.DisplayNameFor(model => model.MSSV)
    </td>
    <td style="text-align:center">
    @Html.DisplayFor(modelItem => item.TEN_SV)
    </td>
    <td style="text-align:center">
    @Html.DisplayFor(modelItem => item.NGAYSINH)
    </td>
    <td style="text-align:center">
    @Html.DisplayFor(modelItem => item.GIOITINH)
    </td>
    <td style="text-align:center">
    @Html.DisplayFor(modelItem => item.LOP.MALOP)
    </td>
    <td style="text-align:center">
    @Html.DisplayFor(modelItem => item.KHOA.TENKHOA)
    </td>
    <td style="text-align:center">
    <button class="btn btn-success">@Html.ActionLink("Sửa", "Edit", new { id = item.MSSV }) </button>
    <button class="btn btn-warning">@Html.ActionLink("Xem", "Details", new { id = item.MSSV }) </button>
    <button class="btn btn-danger"> @Html.ActionLink("Xóa", "Delete", new { id = item.MSSV })</button>
    </td>
    </tr>
    }

    </table>
    <script type="text/javascript">
    $(document).ready(function () {
    $('.btn-primary').click(function () {

    var url = $('#myModal').data('url');
    $.get(url, function (data) {
    $('#myModal').html(data);
    $('#myModal').modal('show');
    });

    });
    });
    </script>

    </div>


    Help me, Thank!!!

    Tuesday, May 21, 2019 11:31 AM

All replies

  • User-474980206 posted

    what doesn't work?

    Tuesday, May 21, 2019 2:43 PM
  • User1520731567 posted

    Hi Anh Minh,

    Which step did you make a mistake?

    PartialView or httppost action?

    public ActionResult Create(FormCollection collection, SINHVIEN sinhvien)

    Did your collection and sinhvien has value?

    You could also try to use:

    [HttpPost]
    public ActionResult Create(FormCollection collection, SINHVIEN sinhvien)
    {
    if (ModelState.IsValid) { db.SINHVIENs.Add(sinhvien); db.SaveChanges(); return RedirectToAction("Index", "Sinh"); }
    ViewBag.ID_LOP = new SelectList(data.SINHVIENs.ToList().OrderBy(n => n.ID_LOP), "ID_LOP", "TENLOP");
    return View(sinhvien);

    }

    Best Regards.

    Yuki Tao

    Wednesday, May 22, 2019 6:05 AM