0

对于我正在开发的应用程序,我为正在开发的视图提供了以下 Razor 代码:

@Html.InputFor(m => m.Property1);   // A date
@Html.InputFor(m => m.Property2);   // Some other date
@Html.InputFor(m => m.SomeOtherProperty);  // Something else.
<a href='#' id='some-button'>Button Text Here</a>

<!-- SNIP: Extra code that dosen't matter -->

<script>
  var $someButton = $('#some-button');

  $(document).ready(function () {
    $someButton.click(function (e) {
      e.preventDefault();
      window.open('@Url.Action("Foo", "Home", new {p1 = Model.Property1, p2 = Model.Property2, pX = Model.SomeOtherProperty})', '_blank');
    });
  });
</script>

...根据评论,我检查了呈现的 HTML。正如预期的那样,这些值带有值......

<input name="Property1" data-val="true" data-val-required="(Required)" type="text" value="1/1/2013">
<input name="Property2" data-val="true" data-val-required="(Required)" type="text" value="4/11/2013">
<input name="SomeOtherProperty" data-val="true" data-val-required="(Required)" type="text" value="42">
<a href='#' id='some-button'>Button Text Here</a>

<script>
  var $someButton = $('#some-button');

  $(document).ready(function () {
    $someButton.click(function (e) {
      e.preventDefault();
      window.open('http://localhost:xxxx/Home/Foo?p1=1%2F1%2F2013&amp;p2=4%2F11%2F2013&amp;pX=42', '_blank');
    });
  });
</script>

...在服务器端...

public ActionResult Foo(string p1, string p2, string pX)
{
  var workModel = new FooWorkModel
  {
    Property1 = p1,
    Property2 = p2,
    SomeOtherProperty = pX
  };

  // Do something with this model, dosen't really matter from here, though.
  return new FileContentResult(results, "application/some-mime-type");
}

我注意到只有第一个参数 ( p1) 从前端获取值;我所有的其他参数都被传递了空值!

问题:为什么为这些其他字段分配了一些值时,ActionResult 被传递空值?或者,一个免费的问题:为什么只有第一个参数成功传递了它的值,而其他所有参数都失败了?

4

1 回答 1

2

该问题是由Url.Action(). (来源:如何将正确的 Url.Action 传递给 JQuery 方法而没有额外的 & 麻烦?

只需在@Html.Raw()周围添加一个调用Url.Action(),数据就会按预期流动。

 window.open('@Html.Raw(Url.Action("Foo", "Home", new {p1 = Model.Property1, p2 = Model.Property2, pX = Model.SomeOtherProperty}))', '_blank');
于 2013-04-11T21:06:08.580 回答