0

我正在尝试将我的模型传递给局部视图。当它被传入时,我想设置参数的值。当我运行它时,我得到:

Object reference not set to an instance of the object

怎么能这么说,因为我通过了这个模型?

@using passTextbox.Models
@model passTextbox.Models.MyViewModel

@Html.TextBoxFor(t=>t.someobject)

@{ Html.RenderAction("test", new MyViewModel() { someobject = "$('#somevalue')" });}

 public class MyViewModel
 {
        public string someobject { get; set; }
 }

部分视图:

@model passTextbox.Models.MyViewModel
<h2>test</h2>
<script src="../../Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        var tb = @Model.somevalue;
        tb.val("new value");
    });
</script>
4

1 回答 1

0

尝试使用Html.RenderPartialinstad ofHtml.RenderAction

public class MyViewModel
    {
        public string someObject { get; set; }
    }

在视图中

@{Html.RenderPartial("partial", new MvcApplication1.Controllers.MyViewModel() { someObject = "test" });}

最后在部分视图中

 @model MyViewModel

<h2>test</h2>
<script type="text/javascript">
    $(document).ready(function () {
        var tb ="@Model.someObject";
        alert(tb);
    });
</script>
于 2012-09-06T01:50:14.547 回答