【问题标题】:Call HttpPost with Ajax.BeginForm not work使用 Ajax.BeginForm 调用 HttpPost 不起作用
【发布时间】:2021-12-03 05:54:27
【问题描述】:

我尝试使用 Ajax 从控制器调用方法,但它从不进入方法,我导入不显眼并在 Bundle 中验证,并始终刷新所有页面。我是第一次在 C# 中使用 vb.net,也许这是一个基本错误,但我搜索了很多并尝试了不同的方法,但一无所获。谢谢你的时间,我希望有人能帮助我 更新:我在母版页中有一个表格,会产生问题 D: 查看

@model PlanificacionOperacional.Models.FiltroView
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section Styles
{
    @Styles.Render("https://cdn.datatables.net/1.11.3/css/jquery.dataTables.min.css")
    @Styles.Render("https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.14/dist/css/bootstrap-select.min.css")

}
<div class="card-header header-elements-inline" style="padding-top:0;"><h4 class="card-title">Muestreos</h4></div>
<fieldset>
    @using (Ajax.BeginForm("Filtrar", "Muestreo",
                                new AjaxOptions
                                {
                                    UpdateTargetId = "tabless",
                                    HttpMethod = "POST"
                                }))
    {
        <div class="form-group">

            @Html.DropDownListFor(model => model.predio, new SelectList(ViewBag.formularios, "id", "nombre"), new { @class = "selectpicker", data_live_search = "true", title = "Predio" })
            @Html.DropDownListFor(model => model.zona, new SelectList(ViewBag.zonas, "id", "nombre"), new { @class = "selectpicker", data_live_search = "true", title = "Zona" })
            @Html.DropDownListFor(model => model.area, new SelectList(ViewBag.areas, "id", "nombre"), new { @class = "selectpicker", data_live_search = "true", title = "Area" })
        </div>
        <div class="form-group" style="align-self:center;">
            @*<input type="submit" value="Filtrar" class="btn btn-primary" style="width:150px;" />*@
            <button type="submit">Filtrar</button>
        </div>
    }
</fieldset>
<div id="tabless" class="card-body" style="">
    <table id="table_id" class="display">
        <thead>
            <tr>
                <th></th>
                <th>ID Muestro</th>
                <th>Area</th>
                <th>Id Predio</th>
                <th>Predio</th>
                <th>Formulario</th>
                <th>APLA</th>
                <th>Total Parcelas</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in ViewBag.muestreos)
            {
                <tr>
                    <td><input type="checkbox" id="cbox2" value="second_checkbox"></td>
                    <td>@item.IdMuestreo</td>
                    <td>@item.Area</td>
                    <td>@item.IdPredio</td>
                    <td>@item.NomPredio</td>
                    <td>@item.Formulario</td>
                    <td>@item.APLA</td>
                    <td>@item.TotalParcela</td>
                </tr>
            }

        </tbody>
    </table>
</div>
@section JavaScriptToFooter {
    @Scripts.Render("https://cdn.datatables.net/1.11.3/js/jquery.dataTables.min.js")
    @Scripts.Render("https://cdn.jsdelivr.net/npm/bootstrap-select@1.13.14/dist/js/bootstrap-select.min.js")
    <script>
        $(document).ready(function () {
            $('#table_id').DataTable();
            $('.selectpicker').selectpicker();
        });
    </script>
    @Scripts.Render("~/bundles/jqueryval")

}

控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PlanificacionOperacional.Models;
using PlanificacionOperacional.Repository;

namespace PlanificacionOperacional.Controllers
{
    public class MuestreoController : Controller
    {
        // GET: Muestreo
        public ActionResult Index()
        {
            MuestreoRepository muestreo = new MuestreoRepository();
            List<Muestreo> muestreos = muestreo.TraeMuestreo();
            List<Filtro> predios = muestreo.TraePredios();
            List<Filtro> zonas = muestreo.TraeZonas();
            List<Filtro> areas = muestreo.TraeAreas();
            ViewBag.muestreos = muestreos;
            ViewBag.formularios = predios;
            ViewBag.zonas = zonas;
            ViewBag.areas = areas;
            return View();
        }        
        [HttpPost]
        public ActionResult Filtrar(FiltroView model)
        {
            int predio = Convert.ToInt32(Request["predio"].ToString());
            int zona = Convert.ToInt32(Request["zona"].ToString());
            return View();
        }
        // GET: Muestreo/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: Muestreo/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Muestreo/Create
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Muestreo/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        // POST: Muestreo/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Muestreo/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: Muestreo/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}

型号

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PlanificacionOperacional.Models
{
    public class FiltroView
    {
        public int zona { get; set; }
        public long predio { get; set; }
        public int area { get; set; }
    }
}

【问题讨论】:

  • 您的浏览器是否发送请求?这个请求的 URI 是什么?要检查这一点,请打开浏览器的开发者工具并点击网络标签。
  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: c# asp.net-ajax


【解决方案1】:

您正在使用 MVC,但您没有任何模型。没有模型,您将永远无法提交表单。所以首先创建一个模型

public ViewModel
{
public int ZonaId {get; set;]
public int AreaId {get;set;}
.... and so on
}

在这个视图之后

@model ViewModel

......

  @Html.DropDownListFor(model=>model.PredioId, new SelectList(ViewBag.formularios, "id", "nombre"), new { @class = "selectpicker", data_live_search = "true", title = "Predio" })
  @Html.DropDownListFor(model=>model.ZonaId, new SelectList(ViewBag.zonas, "id", "nombre"), new { @class = "selectpicker", data_live_search = "true", title = "Zona" })
  @Html.DropDownListFor(model=>model.AreaId, new SelectList(ViewBag.areas, "id", "nombre"), new { @class = "selectpicker", data_live_search = "true", title = "Area" })

.... and so on

动作

 public ActionResult Filtrar(ViewModel model)
{
int predioId = model.PredioId;
int zonaId = model.ZonaId;
.... your code
}

PS。 摆脱你所有的观点。它看起来很不专业,很难维护这种代码。在 ViewModel 中包含所有内容。

public ActionResult Index()
        {
           MuestreoRepository muestreo = new MuestreoRepository();
           var model=new ViewModel{
          
            muestreos = muestreo.TraeMuestreo(),
            predios = muestreo.TraePredios();
             zonas = muestreo.TraeZonas();
             areas = muestreo.TraeAreas();
            ....and so on
           }
            return View(model);
        }        

【讨论】:

  • 按照你说的修改,还是进不了功能,我更新帖子
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-08
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多