如何使用MVC3中的c#代码使标签可见?

时间:2013-11-26 14:04:58

标签: c# asp.net-mvc-3 uiviewcontroller label findcontrol

以下是我的视图页面中的标签

 <label id="back"  onclick="BacktoSubmit()"  style="cursor:pointer; text-decoration:underline; color:Blue; padding-left:670px; visibility:hidden;">Back to list</label>

以下是我的控制器方法,

   public ActionResult TRSkillGradeAvailability(FormCollection frmCollection, string Submit, string Search, string TabType)
    {
        try
        {
            string strLoggedUser = GetLogUser();
            TRSkillGradeAvailabilityModel objTRSkillGradeAvailabilityModel = new TRSkillGradeAvailabilityModel();
            TRSkillGradeAvailabilityModel objTRSkillGradModel = new TRSkillGradeAvailabilityModel();
            TravelReadyCommonModel objTravelReadyCommonModel = new TravelReadyCommonModel();
            string strVertical = string.Empty, strGeo = string.Empty, strGrade = string.Empty, strSkill = string.Empty;
            strVertical = Convert.ToString(frmCollection["ddlVertical"]);
            strGeo = Convert.ToString(frmCollection["ddlGeo"]);
            strGrade = Convert.ToString(frmCollection["ddlGrade"]);
            strSkill = Convert.ToString(frmCollection["ddlSkill"]);
            AssignViewBag(strVertical, strGeo, strGrade, strSkill);


            var button = Submit ?? Search;
            if (button != "Submit")
            {

                if (button == null)
                {
                    button = "Search";
                }
            }
            if (TabType == "1")
            {
                TempData["Tab"] = "1";
                if (button == "Search")
                {                       
                    Label lbl = new Label();
                    lbl.FindControl("back").Visible = true;
                    string strSearchId = string.Empty;
                    strSearchId = Convert.ToString(frmCollection["txtSearch"]).Trim();
                    objTRSkillGradeAvailabilityModel = objTRSkillGradeAvailabilityModel.GetTRPoolData(strVertical, strGeo, strSkill, strGrade, 3, strSearchId, strLoggedUser);
                }
                else if (button == "Submit")
                {
                    if (strVertical == TravelReadyContants.StaticVariable.AllVertical)
                        objTRSkillGradeAvailabilityModel = objTRSkillGradeAvailabilityModel.GetTRPoolData(strVertical, strGeo, strSkill, strGrade, 1, string.Empty, strLoggedUser);
                    else
                        objTRSkillGradeAvailabilityModel = objTRSkillGradeAvailabilityModel.GetTRPoolData(strVertical, strGeo, strSkill, strGrade, 2, string.Empty, strLoggedUser);
                }
            }
            else if (TabType == "2")
            {
                TempData["Tab"] = "2";
                objTRSkillGradeAvailabilityModel = objTRSkillGradeAvailabilityModel.GetMyPoolData(strLoggedUser);
                if (button == "Search")
                {
                    string strSearchId = string.Empty;
                    strSearchId = Convert.ToString(frmCollection["txtSearch"]).Trim();
                    objTRSkillGradeAvailabilityModel.lstTRSkillGradeAvailabilty = objTRSkillGradeAvailabilityModel.lstTRSkillGradeAvailabilty.FindAll(List => List.AssocId == strSearchId);
                }
                else if (button == "Submit")
                {
                    if (strVertical != TravelReadyContants.StaticVariable.AllVertical)
                        objTRSkillGradeAvailabilityModel.lstTRSkillGradeAvailabilty = objTRSkillGradeAvailabilityModel.lstTRSkillGradeAvailabilty.FindAll(List => List.Geo == strGeo && List.Vertical == strVertical && List.SkillName == strSkill && List.GradeName == strGrade);
                    else
                        objTRSkillGradeAvailabilityModel.lstTRSkillGradeAvailabilty = objTRSkillGradeAvailabilityModel.lstTRSkillGradeAvailabilty.FindAll(List => List.Geo == strGeo && List.SkillName == strSkill && List.GradeName == strGrade);
                }
            }

            GetdropdownValues(ref objTravelReadyCommonModel, ref objTRSkillGradModel, 1, string.Empty);
            objTRSkillGradeAvailabilityModel.LoggedInUser = strLoggedUser;
            objTRSkillGradeAvailabilityModel.lstGeo = objTravelReadyCommonModel.lstGeo;
            objTRSkillGradeAvailabilityModel.lstGrade = objTRSkillGradModel.lstGrade;
            objTRSkillGradeAvailabilityModel.lstSkill = objTRSkillGradModel.lstSkill;
            objTRSkillGradeAvailabilityModel.DefaultGeo = objTRSkillGradModel.DefaultGeo;
            objTRSkillGradeAvailabilityModel.DefaultGrade = objTRSkillGradModel.DefaultGrade;
            objTRSkillGradeAvailabilityModel.DefaultSkill = objTRSkillGradModel.DefaultSkill;

            GetdropdownValues(ref objTravelReadyCommonModel, ref objTRSkillGradModel, 2, strGeo);
            objTRSkillGradeAvailabilityModel.lstVerticals = objTravelReadyCommonModel.lstVerticals;
            objTRSkillGradeAvailabilityModel.DefaultVertical = objTRSkillGradModel.DefaultVertical;

            return View(objTRSkillGradeAvailabilityModel);
        }
        catch (Exception ex)
        {
            ILogManager LogManager = new LogManager();
            var frame = new StackFrame(0);
            LogManager.CallLogging(frame, ex.Message, ex.StackTrace);
            return RedirectToAction("Error", "Common");
        }
    }

在我用过的方法中,

     Label lbl = new Label();
     lbl.FindControl("back").Visible = true;

此代码在单击搜索按钮时显示标签,但显示'NullReferenceException'如何在单击搜索按钮时以另一种方式解决此问题以使标签可见?

2 个答案:

答案 0 :(得分:0)

您可以向类型字符串的模型添加一个属性,如可见

public string Visibility { get; set; }

设置此模型属性

if (button == "Search")
{
         objTRSkillGradeAvailabilityModel.Visibility = "visible";         
}

else
      objTRSkillGradeAvailabilityModel.Visibility = "hidden";         

并在视野中

<label id="back" name="back" style="visibility:@Model.Visibility"/>

答案 1 :(得分:0)

@ Html.Label(“”,“无效的帐户ID或未付款。”,htmlAttributes:new {@ class =“ control-label col”,style =“ color:#ff0000”,hidden =“ false” })