【问题标题】:sending value of model property as an ajax parameter将模型属性的值作为 ajax 参数发送
【发布时间】:2016-02-25 14:01:19
【问题描述】:

我的视图页面中有以下表格

@using (Html.BeginForm())
{
   @Html.EditorFor(model => model.Product_ID)

   <input id="idd" type="file" class="file">

   // form sumbit button
   <input type = "submit" value="Create" /> 

}

我想在单击此表单的提交按钮之前将上面插入的Product_ID 作为 ajax 对象路由值发送。因为我创建了它这样的product_ID = Model.Product_ID 但这个product_ID 得到空。

$('#idd').click(function () {

    $.ajax({
        url: '@Url.Action("CreateDirectory", "Home",new { product_ID = Model.Product_ID })',
        type: "POST",

控制器

    [HttpPost]
    public JsonResult CreateDirectory(string product_ID)
    {
      ...........
    }

编辑

在上面的脚本中使用 alert 我尝试显示 Product_ID

alert('@Model.Product_ID');

相同的结果没有显示价值

【问题讨论】:

  • 有一个data参数,用于POST数据。
  • 我可以像这样使用&lt;input id="idd" type="file" class="file" data="@Model.Product_ID"&gt; 并在ajax 中调用data: data-Product_ID, 吗?

标签: c# ajax asp.net-mvc asp.net-mvc-3 jsonresult


【解决方案1】:

@Url.Action() 是剃须刀代码,它在发送到视图之前在服务器上进行评估,因此如果Model.Product_ID 的初始值为null,那么您的代码将仅呈现url: '/Home/CreateDirectory'。生成后更改文本框中的值不会更新 url 的值。

改为使用 ajax data 选项来传递文本框的值

$('#idd').click(function () {
    $.ajax({
        url: '@Url.Action("CreateDirectory", "Home")', // modify
        data: { product_ID: $('#Product_ID').val() }, // add this
        type: "POST",

【讨论】:

  • 一旦我添加了您的解决方案,然后单击该功能,我将看到弹出窗口i.imgur.com/fa7lX8I.jpg
  • 这是我使用的ajax代码bitbucket.org/snippets/Common_Admin/epGKr有什么问题
  • 首先你需要删除contentType: "application/json; charset=utf-8",(你没有发送json)。其次,警报只是显示您的控制器发回的内容,即response.responseText 的值,因此CreateDirectory() 方法中的代码显然存在问题
  • 实际上,由于contentType 选项,它会抛出一个500 (Internal Server Error) 并且您只是看到该错误的结果。
  • 我将调试点放在控制器方法中,但是一旦我单击它不指向此方法,我也尝试使用警报like this new edit of ajax 打印产品ID。这是我的controller method,它之前工作得很好
【解决方案2】:

因为您的操作只接受 [HttpPost] 数据 并且您正在查询字符串 [HttpGet]

中发送变量
@Url.Action("CreateDirectory", "Home",new { product_ID = Model.Product_ID })

网址是下一个:

~/Home/CreateDirectory?product_ID = ....

用 [HttpGet] 替换 [HttpPost] 属性,或者直接删除它

并设置 ajax {type: "GET"}

或者:

$.ajax({
        url: '@Url.Action("CreateDirectory", "Home")',
        type: "POST",
        data: { product_ID: '@(Model.Product_ID)' }

或者:

$.ajax({
        url: '@Url.Action("CreateDirectory", "Home")',
        type: "POST",
        data: { product_ID: $('#Product_ID').val() }

【讨论】:

  • 一个HttpPost仍然可以通过uri接收参数
【解决方案3】:

在这一行中,您为 product_ID 提供了模型在页面中呈现的初始值,因此它可能为 null

url: '@Url.Action("CreateDirectory", "Home",new { product_ID = Model.Product_ID })'

试试这个

url: '@Url.Action("CreateDirectory", "Home")' + '?product_ID=' + $("Product_ID").val()

编辑 也许你也需要这个

 [HttpPost]
public JsonResult CreateDirectory([FromUri]string product_ID)
{
  ...........
}

【讨论】:

  • 这是错误答案,mvc动作有属性HttpPost,然后不接受get参数
  • 所以这样做[HttpPost] public JsonResult CreateDirectory([FromUri]string product_ID) { ........... } 并且HttpPost 可以从uri 接收参数
  • 如果仍然没有得到值,你能告诉我请求发生了什么吗?以及请求的完整 uri?
【解决方案4】:

您的代码应该可以正常工作。

@model ProductInfoVM
@using (Html.BeginForm())
{
    @Html.EditorFor(model => model.Product_ID)

}
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<input id="idd" type="file" class="file">

<script type="text/javascript">

    $(function() {

        $('#idd').click(function() {

            $.ajax({
                url: '@Url.Action("Index", "Home", new {productID = @Model.Product_ID})',
                type: "POST"
            });
        });

    })

</script>

假设您有一个接受productID 的 HttpPost 操作方法

[HttpPost]
public ActionResult Index(string productId)
{
   // to do : do something
}

我能想到的唯一原因是 NULL 出现的原因是,当您呈现页面时,您的 Model.Product_ID 本身就是 NULL。因此,我会查看您的 GET 操作方法,看看您是否正确设置了视图模型的 Product_ID 属性值。

【讨论】:

  • &lt;script src="~/Scripts/jquery-1.8.2.min.js"&gt;&lt;/script&gt; 我还需要在视图页面中添加这个吗,因为我在布局中添加了这一行,然后我在视图页面的 tp 中引用了该布局@Scripts.Render("~/Scripts/jquery.min.js")
  • 如果布局中有,则不需要在页面中添加。
  • 我使用了上面的代码,它运行良好。您可能有一些其他代码破坏了这种行为。复制上面答案中的代码并尝试(仅此代码)
  • 试图打印产品 ID ,我像这样插入 $('#idd').click(function () { alert('@Model.Product_ID');.... 我得到了 null
猜你喜欢
  • 1970-01-01
  • 2012-08-21
  • 2014-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 2021-05-18
相关资源
最近更新 更多