在提交时获取下拉列表选择的值并将其传递给视图中的@helper?需要更优化的方式
Get dropdown list selected value on submit and pass it to @helper in view? need more optimized way
在我的 mvc 应用程序中:
@using (Html.BeginForm())
{
@Html.DropDownList("ddllanguage", Model.Language, new {@style = "width:200px;" })
<input type="submit" value="submit" />
}
<h4>@MyHelper.Translate("Welcome","EN")</h4>
在按钮提交时,我想将所选下拉字符串的值传递给@MyHelper.Translate 方法而不是硬编码"EN"。
在 MVC 中有没有办法做到这一点?否则我应该使用简单的 ViewData
?
设置ViewData value from controller and pass it to view
在控制器中
public ActionResult Index(string ddllanguage)
{
if (string.IsNullOrEmpty(ddllanguage))
{
ViewData["languageCode"] = "EN";
}
else
{
ViewData["languageCode"] = ddllanguage;
}
in View
<h4>>@MyHelper.Translate("Welcome", "", ViewData["languageCode"].ToString())</h4>
I need more optimized way , please suggest.
在你的 cshtml 上:
@using model Namespace.MyModel
@using (Html.BeginForm())
{
@Html.DropDownListFor(model => model.Language, model.Langauages, new {@id = "ddlLanguages", @style = "200px"})
<input type="submit" id="submit" value="Submit" />
}
<!-- On POST, this will display -->
@if (Request.Method.Equals("POST"))
{
MyHelper.Translate("Welcome", model.Language)
}
在你的控制器中:
// HTTP GET
public ActionResult Index ()
{
return ActionResult(new MyModel());
}
// HTTP POST
[HttpPost]
public ActionResult Index (MyModel model)
{
return ActionResult(model);
}
在我的 mvc 应用程序中:
@using (Html.BeginForm())
{
@Html.DropDownList("ddllanguage", Model.Language, new {@style = "width:200px;" })
<input type="submit" value="submit" />
}
<h4>@MyHelper.Translate("Welcome","EN")</h4>
在按钮提交时,我想将所选下拉字符串的值传递给@MyHelper.Translate 方法而不是硬编码"EN"。
在 MVC 中有没有办法做到这一点?否则我应该使用简单的 ViewData
?
设置ViewData value from controller and pass it to view
在控制器中
public ActionResult Index(string ddllanguage)
{
if (string.IsNullOrEmpty(ddllanguage))
{
ViewData["languageCode"] = "EN";
}
else
{
ViewData["languageCode"] = ddllanguage;
}
in View
<h4>>@MyHelper.Translate("Welcome", "", ViewData["languageCode"].ToString())</h4>
I need more optimized way , please suggest.
在你的 cshtml 上:
@using model Namespace.MyModel
@using (Html.BeginForm())
{
@Html.DropDownListFor(model => model.Language, model.Langauages, new {@id = "ddlLanguages", @style = "200px"})
<input type="submit" id="submit" value="Submit" />
}
<!-- On POST, this will display -->
@if (Request.Method.Equals("POST"))
{
MyHelper.Translate("Welcome", model.Language)
}
在你的控制器中:
// HTTP GET
public ActionResult Index ()
{
return ActionResult(new MyModel());
}
// HTTP POST
[HttpPost]
public ActionResult Index (MyModel model)
{
return ActionResult(model);
}