从控制器隐藏/禁用提交按钮

时间:2011-07-15 17:29:26

标签: asp.net-mvc webforms uiviewcontroller

如何从控制器操作隐藏webform按钮?或者我是否在webform中做到了?

隐藏/禁用按钮有一个条件:

if (StudentType != "Senior")
{
Hide Button
}
Display Button

查看:

<form method="post" action="/Student/Dispatch/"> 
    <label for="id">Student Number: </label> 
    <input type="text" name="id" value="" /> <br /><br /> 
    <input type="submit" value="Get Student(xls)" name="xls" /> &nbsp;&nbsp 
    <input type="submit" value="Get Student(pdf)" name="pdf" /> 
</form>

控制器:

[HttpPost] 
public ActionResult Dispatch(string pdf, string id) { 
    if (!string.IsNullOrEmpty(pdf)) { 
        // GetPdf submit button was clicked 
        return StudentPdf(id); 
    } 
    // GetXls submit button was clicked 
    return StudentExcel(id); 
}

1 个答案:

答案 0 :(得分:3)

您可以使用ViewData字典。

控制器:

if (StudentType != "Senior")
{
ViewData["isHideButton"] =true;
}

查看:

   <form method="post" action="/Student/Dispatch/"> 
    <label for="id">Student Number: </label>
     <input type="text" name="id" value="" /> <br /><br /> 
<% bool hideButton= false;
bool.TryParse(ViewData["isHideButton"],hideButton)%>
<%if(!hideButton)
{%>

    <input type="submit" value="Get Student(xls)" name="xls" /> 
<%}%>
    &nbsp;&nbsp <input type="submit" value="Get Student(pdf)" name="pdf" /> 
</form>