本文介绍了在mvc razor应用程序中在哪里编写dropdownlist所选索引更改的编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下垂国家.如果我选择了印度,那么我想将页面重定向到主页.在mvc中怎么可能?


//在cshtml-view页面中,我有这样的编码
< div class ="editor-label">
@ Html.LabelFor(model => model.PropertyTypePropertyTypeId)
</div>
< div class ="editor-field">
@ Html.DropDownList("PropertyTypePropertyTypeId",string.Empty)
@ Html.ValidationMessageFor(model => model.PropertyTypePropertyTypeId)
</div>

//在控制器中

公共ActionResult Create()
{
ViewBag.PropertyTypePropertyTypeId =新的SelectList(context.PropertyTypes,"PropertyTypeId","PTypeName");
返回View();
}
我要在哪里写这些代码?有人可以帮我吗?

i have a drpodown country. if i had select, India then i want to redirect the page to homepage. how can it possible in mvc?


//in cshtml-view page i have coding like this
<div class="editor-label">
@Html.LabelFor(model => model.PropertyTypePropertyTypeId)
</div>
<div class="editor-field">
@Html.DropDownList("PropertyTypePropertyTypeId",string.Empty)
@Html.ValidationMessageFor(model => model.PropertyTypePropertyTypeId)
</div>

// in controller

public ActionResult Create()
{
ViewBag.PropertyTypePropertyTypeId = new SelectList(context.PropertyTypes, "PropertyTypeId", "PTypeName");
return View();
}
where i want to write this codes? Can anyone help me?

推荐答案

//create a method to get Dropdown values 

public ViewResult Index()
        {
            var categories = from c in db.category_master select c;
            ViewData["Categories"] = new SelectList(categories, "ID", "Name");
            return View(db.akshay_category_master.ToList());

        }

// In view 

@Html.DropDownList("Categories", ViewBag.applicationList as SelectList, "Select", new { onchange = "javascript:sendParam();" })

 //Javascript Function 

<script type="text/javascript">
    function sendParam() {

        window.location = "Category/CategoryDetails/" + jQuery("#Categories").val();

    }

  // that Method in Controller

  public ViewResult CategoryDetails(int id)
        {

            var Result = from C in db.category_master
                         where C.id == id
                         select C;
            return View("CategoryDetails", Result.ToList());
          
        } 


@Html.DropDownList(
    "PropertyTypePropertyTypeId",
     ViewData["Categories"], // it need to be casted  i think 
    null,
    new { onchange = "document.location.href = '/Controller/MyAction?country=' + this.options[this.selectedIndex].value;" })



您必须在控制器中创建一个动作.



you have to create an action in the controller.

public ActionResult MyAction(string country ) 
            {
                 // return the adequate view depending on the country argument 
                  .....
            }



祝你好运.



and good luck.



这篇关于在mvc razor应用程序中在哪里编写dropdownlist所选索引更改的编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 02:00