将值传递给局部视图

时间:2012-09-06 01:19:14

标签: jquery asp.net-mvc-3 razor

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

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; }
 }

partialview:

@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>

1 个答案:

答案 0 :(得分:0)

尝试使用Html.RenderPartial代替Html.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>