如何在MVC中将值从一个视图传递到另一个视图 [英] How to pass value from one view to another view in MVC

查看:266
本文介绍了如何在MVC中将值从一个视图传递到另一个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在View01中有一个下拉菜单,我需要在view02中将此下拉菜单的选定值设置为lable。任何人都可以通过示例向我解释这个。



我尝试过:



在view1-dropdown中



I have a drop down in View01 and and i need to set selected value of this drop down to lable in view02.Can any one explain me about this with example.

What I have tried:

In view1-dropdown

<select class="form-control"id="n">
                                   <option>Select Type</option>
                                   <option>A </option>
                                   <option>B </option>
                                   <option>C </option>
                                   <option>D </option>
                                   <option>E</option>
                               </select>







View2-lable



< label class =col-md-3 control-label>通知类型< / label>




View2-lable

<label class="col-md-3 control-label">Notification Type</label>

推荐答案

以这种方式解决,



在我看来,



Solved In this Way,

in my view,

@using (Html.BeginForm())
            {
                @Html.DropDownList("type", new List<SelectListItem>
        {

            new SelectListItem {Text = "A", Value ="A "},
            new SelectListItem {Text = "B", Value = "B "},
            new SelectListItem {Text="C", Value = "C "},
             new SelectListItem {Text="D ", Value = "D "},
          
            }, "Select Type", new { @class = "Form-Control" }
        )
                <button type="submit" value="Submit" name="Submit" class="btn green-sharp btn-outline  btn-block sbold uppercase">Proceed</button>
            }





在我的控制器中,





And in my Controller,

[HttpPost]
       public ActionResult gettype (string type)
       {
           string value = type;
           return RedirectToAction("TargetView", "ActionMethod", new { type1 = type});
       }







在TargetView'Action方法中,






In TargetView'Action method,

public ActionResult RequestNPD(string type)
       {
           string q= Request.QueryString["type1"];
           ViewBag.test = q;


           return View();

       }







在目标视图中,






In Target View,

<label class="col-md-3 control-label">@ViewBag.test </label>





$ / b
谢谢!





Thank You !


< select class =form-controlid =droplistrunat =服务器>

<选项>选择类型< /选项>

<选项> A< / option>

<选项> B< /选项>

<选项> C< /选项>

<选项> D< /选项>

< option> E< / option>

< / select>

< label class =col-md-3 control-labelid =label RUNAT = 服务器 >通知

类型< / label>



C#

string droplist = droplist.SelectedValue;

label = droplist;
<select class="form-control"id="droplist" runat="server">
<option>Select Type</option>
<option>A </option>
<option>B </option>
<option>C </option>
<option>D </option>
<option>E</option>
</select>
<label class="col-md-3 control-label" id="label" runat="server"> Notification
Type</label>

C#
string droplist= droplist.SelectedValue;
label= droplist;


这篇关于如何在MVC中将值从一个视图传递到另一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆