简体   繁体   English

在部分视图razor mvc中设置文本框的值

[英]set value for a textbox in partial view razor mvc

I have following scenario: 我有以下场景:

My Index page, uses a layout which has a partial View ebbeded in it. 我的索引页面使用的布局中包含部分视图。 the partial view contains a search text box. 局部视图包含搜索文本框。

For a particular scenario, i need to set the text of the search box with my viewdata[] for index page. 对于特定场景,我需要使用我的viewdata []为索引页面设置搜索框的文本。

is it somehow poosiblein mvc3, asp.net 2010 to set the value of textbox in partial view from the viewpage? 是不是在某种程度上poosiblein mvc3,asp.net 2010在视图页面的局部视图中设置文本框的值?

You could make your partial strongly typed to some view model: 您可以将部分强类型设置为某个视图模型:

@model SearchViewModel
@using (Html.BeginForm())
{
    @Html.LabelFor(x => x.Keywords)
    @Html.EditorFor(x => x.Keywords)
    <button type="submit">OK</button>
}

and then when inserting the partial you could pass this view model: 然后在插入部分时,您可以传递此视图模型:

@Html.Partial("_Search", new SearchViewModel { Keywords = "some initial value" })

or even better the view model of your main view will already have a property of type SearchViewModel and you will be able to call the partial like this: 或者甚至更好的主视图的视图模型已经具有SearchViewModel类型的属性,您将能够像这样调用部分:

@Html.Partial("_Search", Model.Search)

Now obviously in your Index action you no longer need to use any ViewData, but you could directly work with your strongly typed view model: 现在显然在您的Index操作中,您不再需要使用任何ViewData,但您可以直接使用强类型视图模型:

public ActionResult Index()
{
    var model = new MyViewModel
    {
        Search = new SearchViewModel
        {
            Keywords = "some initial value"
        }
    };
    return View(model);
}

You can always make the partial view strongly typed (even if the model is just a string) and pass the value you need. 您始终可以强制键入部分视图(即使模型只是一个字符串)并传递您需要的值。

public class MyModel
{
    public int ValueForView {get;set;}
    public string TextBoxValue {get;set;}
}

-Index.cshtml -Index.cshtml

@model MyModel

@{ Html.RenderPartial("PartialView", Model.TextBoxValue); }

-PartialView.cshtml -PartialView.cshtml

@model string

@Html.TextBoxFor(m => Model)

As I understand your issue, the partial view is in your layout and you need to get data into it. 据我了解您的问题,部分视图位于您的布局中,您需要将数据输入其中。

In this case layouts are processed last but passing data to it your options are somewhat limited. 在这种情况下,布局最后处理但是将数据传递给它,您的选项有些限制。 You can use an ActinFilter or ViewData. 您可以使用ActinFilter或ViewData。 ViewData is the easiest, and also the messiest so I don't recommend it. ViewData是最简单的,也是最混乱的,所以我不推荐它。

ActionFilters would work, but you could just process your partial by simply calling in your layout: ActionFilters可以工作,但你可以通过简单地调用你的布局来处理你的部分:


 @Html.RenderAction("PartialViewAction", "PartialViewController")

Unless I'm missing something I don't believe the other answers addressed that this is in a layout, hence a different issue. 除非我遗漏了一些东西,否则我不相信其他答案会解决这个问题,因为这是一个不同的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM