0

我的控制器(维护)中有这样的代码:

public JsonResult GetGroupPortfolioDetail(string sPFolioGroupCode)
    {
        var result = _portfolioRepository.GetGroupPortfolio(sPFolioGroupCode.Trim());
        var portfolio = new TPFolio();

        portfolio.PFolioCode = result.PortfolioGroupCode;
        portfolio.PFolioSName = result.ShortName;
        portfolio.PFolioFName = result.FullName;
        portfolio.ReportingCurrency = result.Curr;
        portfolio.NPWP = result.NPWP;
        portfolio.IsActive = result.IsActive; 

        return Json(portfolio, JsonRequestBehavior.AllowGet);
    }

和 viewmodel (GroupPortfolioViewModel) 中的代码:

public class GroupPortFolio
{

    public string PortfolioGroupCode { get; set; }
    public string PortfolioCode { get; set; }
    public string ShortName { get; set; }
    public string FullName { get; set; }
    public float Seq { get; set; }
    public string IsActive { get; set; }
    public string NPWP { get; set; }
    public string Curr { get; set; }
    public string TimestampID { get; set; }
}

和我的存储库文件夹名称文件中的查询代码:(portfolioRepository):

public GroupPortFolio GetGroupPortfolio(string sPFolioGroupCode)
    {
        string query = "SELECT DISTINCT B.PFolioGroupCode AS PortfolioGroupCode, A.PFolioSName AS ShortName , A.PFolioFName AS FullName,  A.IsActive AS Active, A.ReportingCurrency AS Curr, A.NPWP AS NPWP, A.TimestampID AS TimestampID ";
        query = query + " FROM TPFolio As A INNER JOIN TPFolioGroup As B ON A.PFolioCode = B.PFolioGroupCode WHERE (B.PFolioGroupCode = '" + sPFolioGroupCode + "')";

        return _db.ExecuteQuery<GroupPortFolio>(query).SingleOrDefault();
    }

和 View (GroupPortfolio) 中的代码:

function loadGroupPortFolio(sPFolioGroupCode) {

    $.ajax({
        url: '@Url.Action("GetGroupPortfolioDetail", "Maintenance")',
        data: 'sPFolioGroupCode=' + sPFolioGroupCode,
        success: function (result) {
            $('#PortfolioGroupCode').val(result.PFolioCode);
            $('#PortfolioSName').val(result.PFolioSName);
            $('#PortfolioFName').val(result.PFolioFName);
            $('#Currency').val(result.ReportingCurrency);
            $('#NPWP').val(result.NPWP);
            var isactive = result.IsActive;
            $("input[name='RadioActive']").each(function () {
                if ($(this).val() == isactive) {
                    $(this).attr("checked", "checked");
                    //$(this).prop("checked", "checked");
                }
            });

            TimeStampIDPortfolio = result.TimestampID;
        }
    });
}

和 html :

<fieldset>
                    <div class="row">
                        <section class="col col-lg-2">
                            <label class="label">Code</label>
                            <label class="input">
                                <input type="text" name="PortfolioCode" id="PortfolioGroupCode" />
                                <b class="tooltip tooltip-bottom-right">Portfolio Code</b>
                            </label>
                        </section>
                        <section class="col col-lg-2">
                            <label class="label">Short Name</label>
                            <label class="input">
                                <input type="text" name="PortfolioSName" id="PortfolioSName" />
                            </label>
                        </section>
                        <section class="col col-lg-2">
                            <label class="label">Full Name</label>
                            <label class="input">
                                <input type="text" name="PortfolioFName" id="PortfolioFName" />
                            </label>
                        </section>
                    </div>
                    <div class="row">
                        <section class="col col-lg-2">
                            <label class="label">Report Currency</label>
                            <label class="select">
                                @Html.DropDownListFor(model => model.Currency, Model.Currencies, new { @class = "input-sm" })
                                <i></i>
                            </label>
                        </section>
                        <section class="col col-lg-2">
                            <label class="label">NPWP</label>
                            <label class="input">
                                <input type="text" name="NPWP" id="NPWP" />
                            </label>
                        </section>
                        <section class="col col-lg-2">
                            <label class="label">Active</label>
                            <div class="inline-group">
                                <label class="radio">
                                    <input type="radio" name="RadioActive" class="RadioActive" value="T" />
                                    <i></i>
                                    Yes
                                </label>
                                <label class="radio">
                                    <input type="radio" name="RadioActive" class="RadioActive" value="F" />
                                    <i></i>
                                    No
                                </label>
                            </div>
                        </section>
                    </div>
                </fieldset>

问题是我无法从 Controller 中的“portfolio.IsActive”中获得价值。所以当我在浏览器中运行它时,未选中 IsActive 的单选按钮。我得到 IsActive 的值为 Null。不是 T 或 F。

请帮助我,你知道我的代码有什么错误吗?

4

1 回答 1

2

IsActive为空,因为您的 SQL 查询将 IsActive 返回为Active

SELECT DISTINCT B.PFolioGroupCode AS PortfolioGroupCode
              , A.PFolioSName AS ShortName 
              , A.PFolioFName AS FullName
              , A.IsActive AS Active /* <-- see here*/
              -- ...
FROM TPFolio As A ...

因此,要么将您的 ViewModel 从更改IsActiveActive或更改 SQL 查询中的别名。

加:使用参数!想想小鲍比桌。

于 2017-07-19T07:17:41.827 回答