c#mvc helper extension Html.DisplayFor

时间:2016-11-01 16:34:48

标签: c# asp.net-mvc extension-methods displayfor

几小时后。以后互相干扰。头发拉得很晚。

Prs_Role_ID_Deleted是一个int。

在我的Details.cshtml中,如果model.Prs_Role_ID_Deleted = 0(zeri)的值,我试图让它输出一个“”(空字符串)。

问题是我无法获得model =>的值model.Prs_Role_ID_Deleted。 我可以得到属性名称。 我可以得到model.Prs_Role_ID_Deleted。

我无法获得model.Prs_Role_ID_Deleted的值,该值应等于22。

@Html.ZZZ(model => model.Prs_Role_ID_Deleted)


public static MvcHtmlString ZZZ<TModel, TValue>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TValue>> expression)
{
    var id = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(ExpressionHelper.GetExpressionText(expression));
    return "whatever";
}

我开始认为无法确定价值。

谢谢SLaks。这就是解决方案。

为其他开发者发布解决方案。

SLACK建议之后 @ Html.DisplayForWithID_ZeroIsBlank(model =&gt; model.Prs_Role_ID_Deleted,“span”)

public static MvcHtmlString DisplayForWithID_ZeroIsBlank(此HtmlHelper助手,Expression&gt;表达式,字符串wrapperTag =“span”) {     var id = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldId(ExpressionHelper.GetExpressionText(expression));     TModel model =(TModel)helper.ViewContext.ViewData.ModelMetadata.Model;     var ctlValue = expression.Compile()(helper.ViewData.Model);     string OutputValue =“傻开发者。这只适用于int。”;     类型type = ctlValue.GetType();     if(type.Equals(typeof(int)))     {         string s = ctlValue.ToString()。Trim();         if(ctlValue == null || ctlValue.ToString()。Trim()==“0”)         {             OutputValue =“”;         }         其他         {             OutputValue = ctlValue.ToString();         }     }     return MvcHtmlString.Create(string.Format(“&lt; {0} id = \”{1} \“&gt; {2}”,wrapperTag,id,OutputValue,helper.DisplayFor(expression))); }

1 个答案:

答案 0 :(得分:3)

您需要将表达式编译为委托,然后在模型上调用它:

 public class POReceiptEntryGraphExtension : PXGraphExtension<PO.POReceiptEntry>
{

    public PXSelect<PO.POReceipt> NewRevisionPanel;

    public PXAction<PO.POReceipt> ImportAllocations;
    [PXUIField(DisplayName = "Import Allocations", MapEnableRights = PXCacheRights.Update,
                             MapViewRights = PXCacheRights.Update, Enabled = true)]
    [PXButton()]
    public virtual void importAllocations()
    {
        try
        {
            if (Base.transactions.Current != null)
            {
                if (Base.splits.Select().Count == 0)
                {
                    if (this.NewRevisionPanel.AskExt() == WebDialogResult.OK)
                    {
                        const string PanelSessionKey = "ImportStatementProtoFile";
                        PX.SM.FileInfo info = PX.Common.PXContext.SessionTyped<PXSessionStatePXData>().FileInfo[PanelSessionKey] as PX.SM.FileInfo;
                        System.Web.HttpContext.Current.Session.Remove(PanelSessionKey);

                        if (info != null)
                        {
                            byte[] filedata = info.BinData;
                            using (NVExcelReader reader = new NVExcelReader())
                            {
                                Dictionary<UInt32, string[]> data = reader.loadWorksheet(filedata);
                                foreach (string[] textArray in data.Values)
                                {
                                    Base.splits.Insert(new PO.POReceiptLineSplit()
                                    {
                                        InventoryID = Base.transactions.Current.InventoryID,
                                        LocationID = Base.transactions.Current.LocationID,
                                        LotSerialNbr = textArray[2],
                                        Qty = Decimal.Parse(textArray[3])
                                    });
                                }
                            }
                        }
                    }
                }
            }
            Base.Actions["LSPOReceiptLine_binLotSerial"].Press();
        }
        catch (FileFormatException fileFormat)
        {
            throw new PXException(String.Format("Incorrect file format. File must be of type .xlsx", fileFormat.Message));
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}