Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 在@html.dropdownlistfor中设置所选值_Asp.net Mvc 3 - Fatal编程技术网

Asp.net mvc 3 在@html.dropdownlistfor中设置所选值

Asp.net mvc 3 在@html.dropdownlistfor中设置所选值,asp.net-mvc-3,Asp.net Mvc 3,我的视图中有以下代码 <td>@Html.DropDownListFor(e => e[0].Reason, new List<SelectListItem> ( new[] { new SelectListItem { Text = " -Select- ", Value = "-Select-" }, new SelectListItem { Text = " Price ", Value = "Price" },

我的视图中有以下代码

 <td>@Html.DropDownListFor(e => e[0].Reason, new List<SelectListItem>
  (
   new[]
    {
       new SelectListItem { Text = " -Select- ", Value = "-Select-" },
       new SelectListItem { Text = " Price ", Value = "Price" },
       new SelectListItem { Text = " 3P ", Value = "3P" },
       new SelectListItem { Text = " Freight Collect ", Value = "Freight Collect" },
       new SelectListItem { Text = " Billing ", Value = "Billing" },
       new SelectListItem { Text = " Business Closure ", Value = "Business Closure" },
       new SelectListItem { Text = " Customer Service ", Value = "Customer Service" },
       new SelectListItem { Text = " Quality ", Value = "Quality" },
       new SelectListItem { Text = " Service ", Value = "Service" },
       new SelectListItem { Text = " Business Relocate ", Value = "Business Relocate" },
       new SelectListItem { Text = " Change in Relationships ", Value = "Change in Relationships" },
       new SelectListItem { Text = " Different Account Code ", Value = "Different Account Code" },
       new SelectListItem { Text = " Economic Downturn ", Value = "Economic Downturn" },
       new SelectListItem { Text = " BIC ", Value = "BIC" },
       new SelectListItem { Text = " Credit/Collections ", Value = "Credit/Collections" }

        }
        ))</td>
@Html.DropDownListFor(e=>e[0]。原因,新列表
(
新[]
{
新建SelectListItem{Text=“-Select-”,Value=“-Select-”},
新建SelectListItem{Text=“Price”,Value=“Price”},
新建SelectListItem{Text=“3P”,Value=“3P”},
新建SelectListItem{Text=“运费到付”,Value=“运费到付”},
新建SelectListItem{Text=“Billing”,Value=“Billing”},
新建SelectListItem{Text=“Business Closure”,Value=“Business Closure”},
新建SelectListItem{Text=“Customer Service”,Value=“Customer Service”},
新建SelectListItem{Text=“Quality”,Value=“Quality”},
新建SelectListItem{Text=“Service”,Value=“Service”},
新建SelectListItem{Text=“业务重新定位”,Value=“业务重新定位”},
新建SelectListItem{Text=“关系变更”,Value=“关系变更”},
新建SelectListItem{Text=“不同帐户代码”,Value=“不同帐户代码”},
新建SelectListItem{Text=“经济衰退”,Value=“经济衰退”},
新建SelectListItem{Text=“BIC”,Value=“BIC”},
新建SelectListItem{Text=“信用/收款”,Value=“信用/收款”}
}
))
每次页面加载时,所选值都会发生更改

你能告诉我如何设置所选的值吗


提前感谢。

您可以通过模型对象传递所选值。从action方法中,您可以返回模型对象,如下所示

model obj=new model();
obj.Reason="Price";
return view(obj)
在视图中,在加载自身时选择具有值的项目
“Price”

或者可以使用jquery设置所选值

$(document).ready(function(){
   $("#Reason").val("Price");
});

可以通过模型对象传递选定的值。从action方法中,您可以返回模型对象,如下所示

model obj=new model();
obj.Reason="Price";
return view(obj)
在视图中,在加载自身时选择具有值的项目
“Price”

或者可以使用jquery设置所选值

$(document).ready(function(){
   $("#Reason").val("Price");
});

如果要在视图中设置选定值:

@Html.DropDownListFor( e => e[0].Reason, new SelectList( new List<SelectListItem>(){
   new SelectListItem { Text = " -Select- ", Value = "-Select-" },
   new SelectListItem { Text = " Price ", Value = "Price" },
   new SelectListItem { Text = " 3P ", Value = "3P" },
   new SelectListItem { Text = " Freight Collect ", Value = "Freight Collect" },
   new SelectListItem { Text = " Billing ", Value = "Billing" },
   new SelectListItem { Text = " Business Closure ", Value = "Business Closure" },
   new SelectListItem { Text = " Customer Service ", Value = "Customer Service" },
   new SelectListItem { Text = " Quality ", Value = "Quality" },
   new SelectListItem { Text = " Service ", Value = "Service" },
   new SelectListItem { Text = " Business Relocate ", Value = "Business Relocate" },
   new SelectListItem { Text = " Change in Relationships ", Value = "Change in Relationships" },
   new SelectListItem { Text = " Different Account Code ", Value = "Different Account Code" },
   new SelectListItem { Text = " Economic Downturn ", Value = "Economic Downturn" },
   new SelectListItem { Text = " BIC ", Value = "BIC" },
   new SelectListItem { Text = " Credit/Collections ", Value = "Credit/Collections" }
    } , "Value", "Text", e[0].Reason ) )
@Html.DropDownListFor(e=>e[0]。原因,新选择列表(新列表(){
新建SelectListItem{Text=“-Select-”,Value=“-Select-”},
新建SelectListItem{Text=“Price”,Value=“Price”},
新建SelectListItem{Text=“3P”,Value=“3P”},
新建SelectListItem{Text=“运费到付”,Value=“运费到付”},
新建SelectListItem{Text=“Billing”,Value=“Billing”},
新建SelectListItem{Text=“Business Closure”,Value=“Business Closure”},
新建SelectListItem{Text=“Customer Service”,Value=“Customer Service”},
新建SelectListItem{Text=“Quality”,Value=“Quality”},
新建SelectListItem{Text=“Service”,Value=“Service”},
新建SelectListItem{Text=“业务重新定位”,Value=“业务重新定位”},
新建SelectListItem{Text=“关系变更”,Value=“关系变更”},
新建SelectListItem{Text=“不同帐户代码”,Value=“不同帐户代码”},
新建SelectListItem{Text=“经济衰退”,Value=“经济衰退”},
新建SelectListItem{Text=“BIC”,Value=“BIC”},
新建SelectListItem{Text=“信用/收款”,Value=“信用/收款”}
},“值”,“文本”,e[0]。原因)

对于控制器中的设置选定值,请选中回答:

如果要在视图中设置选定值:

@Html.DropDownListFor( e => e[0].Reason, new SelectList( new List<SelectListItem>(){
   new SelectListItem { Text = " -Select- ", Value = "-Select-" },
   new SelectListItem { Text = " Price ", Value = "Price" },
   new SelectListItem { Text = " 3P ", Value = "3P" },
   new SelectListItem { Text = " Freight Collect ", Value = "Freight Collect" },
   new SelectListItem { Text = " Billing ", Value = "Billing" },
   new SelectListItem { Text = " Business Closure ", Value = "Business Closure" },
   new SelectListItem { Text = " Customer Service ", Value = "Customer Service" },
   new SelectListItem { Text = " Quality ", Value = "Quality" },
   new SelectListItem { Text = " Service ", Value = "Service" },
   new SelectListItem { Text = " Business Relocate ", Value = "Business Relocate" },
   new SelectListItem { Text = " Change in Relationships ", Value = "Change in Relationships" },
   new SelectListItem { Text = " Different Account Code ", Value = "Different Account Code" },
   new SelectListItem { Text = " Economic Downturn ", Value = "Economic Downturn" },
   new SelectListItem { Text = " BIC ", Value = "BIC" },
   new SelectListItem { Text = " Credit/Collections ", Value = "Credit/Collections" }
    } , "Value", "Text", e[0].Reason ) )
@Html.DropDownListFor(e=>e[0]。原因,新选择列表(新列表(){
新建SelectListItem{Text=“-Select-”,Value=“-Select-”},
新建SelectListItem{Text=“Price”,Value=“Price”},
新建SelectListItem{Text=“3P”,Value=“3P”},
新建SelectListItem{Text=“运费到付”,Value=“运费到付”},
新建SelectListItem{Text=“Billing”,Value=“Billing”},
新建SelectListItem{Text=“Business Closure”,Value=“Business Closure”},
新建SelectListItem{Text=“Customer Service”,Value=“Customer Service”},
新建SelectListItem{Text=“Quality”,Value=“Quality”},
新建SelectListItem{Text=“Service”,Value=“Service”},
新建SelectListItem{Text=“业务重新定位”,Value=“业务重新定位”},
新建SelectListItem{Text=“关系变更”,Value=“关系变更”},
新建SelectListItem{Text=“不同帐户代码”,Value=“不同帐户代码”},
新建SelectListItem{Text=“经济衰退”,Value=“经济衰退”},
新建SelectListItem{Text=“BIC”,Value=“BIC”},
新建SelectListItem{Text=“信用/收款”,Value=“信用/收款”}
},“值”,“文本”,e[0]。原因)
对于控制器中的设置选定值,请检查答案:

确定,veena

我留下了一条评论,建议如果是我,我会怎么做。但是,您正在根据当前的实现寻找答案,因此,我现在对此有以下看法:

<td>
    @{ var reasonList = new List<SelectListItem>(new[] {
        new SelectListItem { 
            Text=" -Select-",
            Value="-Select- "
        },
        new SelectListItem { 
            Selected = Model[0].Reason.Equals("Price"), 
            Text=" Price",
            Value="Price"
        },
        new SelectListItem {
            Selected = Model[0].Reason.Equals("3P"),
            Text=" 3P",
            Value="3P"
        }
        // etc etc...
      });
    }
    @Html.DropDownListFor(m => m[0].Reason, reasonList)
</td>

@{var reasonList=新列表(新[]{
新建SelectListItem{
Text=“-Select-”,
Value=“-Select-”
},
新建SelectListItem{
所选=模型[0]。原因。等于(“价格”),
Text=“价格”,
Value=“价格”
},
新建SelectListItem{
所选=模型[0]。原因。等于(“3P”),
Text=“3P”,
Value=“3P”
}
//等等等等。。。
});
}
@Html.DropDownListFor(m=>m[0]。原因,原因列表)
但是,我强烈建议您将此逻辑向上移动到控制器/服务代码中,因为当包含此类代码时,视图实际上变得杂乱无章。我还强烈质疑以这种方式检查索引元素的理由。为了简洁起见,我假设您已经指定了索引[1]、[2]等。如果是这种情况,那么您应该真正使用foreach循环,并在代码中按顺序遍历元素。下面是一个例子
@Html.DropDownListFor(model => model.DataBaseFieldToPopulate, ViewBag.MyDropDownListDataWithASelectedItem, "Invitation to select something from the list", new { @class = "a_CSS_class_name_of_your_choosing" })