ASP .Net MVC - 无法从控制器获取值
ASP .Net MVC - can not get value from controller
我的控制器(维护)中有这样的代码:
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);
}
和视图模型 (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。
请帮助我,你知道我的代码有什么错误吗??
IsActive
为空,因为您的 SQL 查询 returns IsActive as 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 从 IsActive
更改为 Active
,要么更改 SQL 查询中的别名。
我的控制器(维护)中有这样的代码:
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);
}
和视图模型 (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。
请帮助我,你知道我的代码有什么错误吗??
IsActive
为空,因为您的 SQL 查询 returns IsActive as 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 从 IsActive
更改为 Active
,要么更改 SQL 查询中的别名。