Asp.net mvc 模型值不可访问
我有一个模型,它保存有关帐户(名称、Id等)的一些详细信息,然后它有一个名为Transaction的类型,它保存有关当前所选帐户事务的信息。然后,一个事务可以有许多事务行。所以我有一个Asp.net mvc 模型值不可访问,asp.net-mvc,Asp.net Mvc,我有一个模型,它保存有关帐户(名称、Id等)的一些详细信息,然后它有一个名为Transaction的类型,它保存有关当前所选帐户事务的信息。然后,一个事务可以有许多事务行。所以我有一个列表属性 我试图使用模型设置下拉列表的值,该值位于list属性中。目前,列表中可以而且必须只有一项 @Html.DropDownListFor(x=>x.Transaction.TransactionLines[0].CategoryId, Model.TransactionReferences.Catego
列表
属性
我试图使用模型设置下拉列表的值,该值位于list属性中。目前,列表中可以而且必须只有一项
@Html.DropDownListFor(x=>x.Transaction.TransactionLines[0].CategoryId, Model.TransactionReferences.Categories, new {@onchange="populateSubCategory()"})
但是,当我运行此命令时,列表默认为列表中的第一项
在调试模式下,当我将鼠标悬停在x.Transaction.TransactionLines[0].CategoryId
上时,它不会显示值。但是当将鼠标悬停在集合上时,Model.TransactionReferences.Categories
,我看到它有一个有效的列表。它只是不会设置所选的值
我做错了吗
它适用于我使用的其他下拉列表,但选择值位于模型的最顶层:
@Html.DropDownListFor(x => x.Transaction.ThirdPartyId, Model.TransactionReferences.ThirdParties, new { @class = "cmbThirdParty form-control", @onchange = "populateDefaults()" })
那一个很好用
注意,手动执行此操作可以:
<select class="form-control" id="cmbCategory" onchange="populateSubCategory()">
<option value="0">Select a One</option>
@foreach (var cat in Model.TransactionReferences.Categories)
{
//var selected = cat.Value == Model.Transaction.TransactionLines[0].CategoryId.ToString() ? "selected" : "";
<option value="@cat.Value">@cat.Text</option>
}
</select>
TransactionReferenceModel保存用于填充下拉列表的所有“引用”数据:
public class TransactionReferenceModel
{
public List<SelectListItem> TransactionTypes { get; set; }
public List<SelectListItem> EntryTypes { get; set; }
public List<SelectListItem> SubCategories { get; set; }
public List<SelectListItem> Categories { get; set; }
public List<SelectListItem> ThirdParties { get; set; }
public List<SelectListItem> CostCentres { get; set; }
}
下面是我填充模型并将其发送到视图的方式:
public class TransactionModel
{
public int BankAccountId { get; set; }
public string BankAccountName { get; set; }
public TransactionContainer Transaction { get; set; }
public TransactionReferenceModel TransactionReferences { get; set; }
public DateTime DefaultDate { get; set; }
}
public ActionResult EditTransaction(int? transactionId, int? bankAccountId)
{
// Create the main view object
var model = new TransactionModel
{
Transaction = new TransactionContainer
{
TransactionLines = new List<TransactionLine>()
}
};
if (transactionId != null) // This is an Edit, as opposed to an Add
{
var item = new TransactionService(currentUserId).GetTransaction(transactionId.Value);
// Populate the Reference object used to populate drop downs.
model.TransactionReferences = PopulateReferenceDate(model.TransactionReferences, item.TransactionLines[0].SubCategoryId);
model.BankAccountId = item.AccountId;
model.BankAccountName = item.Account.FullName;
model.DefaultDate = Session["DefaultDate"] != null
? DateTime.Parse(Session["DefaultDate"].ToString())
: DateTime.UtcNow;
model.Transaction.AccountId = item.AccountId;
model.Transaction.Amount = item.Amount;
model.Transaction.TransactionLines.Add(new TransactionLine
{
Id = item.TransactionLines[0].Id,
CategoryId = item.TransactionLines[0].SubCategory.CategoryId,
CostCentreId = item.TransactionLines[0].CostCentreId,
Notes = item.TransactionLines[0].Notes,
Amount = item.TransactionLines[0].Amount,
SubCategoryId = item.TransactionLines[0].SubCategoryId,
TransactionId = model.Transaction.Id
});
model.Transaction.EntryTypeId = item.EntryTypeId;
model.Transaction.Id = transactionId.Value;
model.Transaction.Notes = item.Notes;
model.Transaction.ThirdPartyId = item.ThirdPartyId;
model.Transaction.TransactionDate = item.TransactionDate;
model.Transaction.TransactionTypeId = item.TransactionTypeId;
}
else
{
// Populate the bank account details
var bank = new BankAccountService(currentUserId).GetBankAccountById(bankAccountId.Value);
model.TransactionReferences = PopulateReferenceDate(model.TransactionReferences, null);
model.BankAccountId = bank.Id;
model.BankAccountName = bank.FullName;
model.Transaction.TransactionLines.Add(new TransactionLine
{
TransactionId = model.Transaction.Id // Link the transaction line to the transaction.
});
var transactionDate = Session["DefaultDate"] != null
? DateTime.Parse(Session["DefaultDate"].ToString())
: DateTime.UtcNow;
// Populate the object to hold the Transaction data, so that we can use it and return it in the view.
model.Transaction.TransactionDate = transactionDate;
}
return View(model);
}
public ActionResult EditTransaction(int?transactionId,int?bankAccountId)
{
//创建主视图对象
var模型=新交易模型
{
事务=新的TransactionContainer
{
TransactionLines=新列表()
}
};
if(transactionId!=null)//这是一个编辑,而不是添加
{
var item=newTransactionService(currentUserId).GetTransaction(transactionId.Value);
//填充用于填充下拉列表的引用对象。
model.TransactionReferences=PopulateReferenceDate(model.TransactionReferences,item.TransactionLines[0]。子类别ID);
model.BankAccountId=item.AccountId;
model.BankAccountName=item.Account.FullName;
model.DefaultDate=会话[“DefaultDate”]!=null
?DateTime.Parse(会话[“DefaultDate”].ToString())
:DateTime.UtcNow;
model.Transaction.AccountId=item.AccountId;
model.Transaction.Amount=项目.金额;
model.Transaction.TransactionLines.Add(新建TransactionLine
{
Id=item.TransactionLines[0]。Id,
CategoryId=item.TransactionLines[0]。子类别.CategoryId,
CostCentreId=item.TransactionLines[0]。CostCentreId,
Notes=item.TransactionLines[0]。备注,
金额=项目。交易行[0]。金额,
SubCategory ID=item.TransactionLines[0]。SubCategory ID,
TransactionId=model.Transaction.Id
});
model.Transaction.EntryTypeId=item.EntryTypeId;
model.Transaction.Id=transactionId.Value;
model.Transaction.Notes=项目.Notes;
model.Transaction.ThirdPartyId=item.ThirdPartyId;
model.Transaction.TransactionDate=item.TransactionDate;
model.Transaction.TransactionTypeId=item.TransactionTypeId;
}
其他的
{
//填充银行帐户详细信息
var bank=新的BankAccountService(currentUserId).GetBankAccountById(bankAccountId.Value);
model.TransactionReferences=PopulateReferenceDate(model.TransactionReferences,null);
model.BankAccountId=bank.Id;
model.BankAccountName=bank.FullName;
model.Transaction.TransactionLines.Add(新建TransactionLine
{
TransactionId=model.Transaction.Id//将事务行链接到事务。
});
var transactionDate=会话[“DefaultDate”]!=null
?DateTime.Parse(会话[“DefaultDate”].ToString())
:DateTime.UtcNow;
//填充对象以保存事务数据,以便我们可以使用它并在视图中返回它。
model.Transaction.TransactionDate=TransactionDate;
}
返回视图(模型);
}
我认为您应该在视图中使用SelectList构造函数,以指示默认值,如下所示:
@Html.DropDownListFor(
x => x.Transaction.TransactionsLines[0].CategoryId,
new SelectList(Model.TransactionReferences.Categories, "Value", "Text", Model.Transaction.TransactionsLines[0].CategoryId)
)
您不限于对集合使用列表public class HomeController : Controller
{
public ActionResult Index()
{
var m = new AccountModel();
m.Transaction = new Transaction();
m.Transaction.TransactionsLines = new List<TransactionsLine>();
m.Transaction.TransactionsLines.Add(new TransactionsLine() { CategoryId = 2 });
m.TransactionReferences = new TransactionReferences();
m.TransactionReferences.Categories = new List<SelectListItem>()
{ new SelectListItem() { Text = "Cat1", Value = "1" },
new SelectListItem() { Text = "Cat2", Value = "2" },
new SelectListItem() { Text = "Cat3", Value = "3" }
};
return View(m);
}
}
公共类HomeController:控制器
{
公共行动结果索引()
{
var m=新的AccountModel();
m、 事务=新事务();
m、 Transaction.TransactionsLines=新列表();
m、 Transaction.TransactionLines.Add(新TransactionLine(){CategoryId=2});
m、 TransactionReferences=新的TransactionReferences();
m、 TransactionReferences.Categories=新列表()
{new SelectListItem(){Text=“Cat1”,Value=“1”},
新建SelectListItem(){Text=“Cat2”,Value=“2”},
新建SelectListItem(){Text=“Cat3”,Value=“3”}
};
返回视图(m);
}
}
您能显示型号代码吗?已添加。希望它有意义。我对MVC比较陌生,所以可能走错了方向。这产生了同样的问题。未选择所选值。此外,我将其设置为的属性(CategoryId)似乎没有问题。我刚刚在我的模型中添加了一个新属性,将其硬编码为我知道在列表中的值,但它仍然在d中设置了所选项
@Html.DropDownListFor(
x => x.Transaction.TransactionsLines[0].CategoryId,
new SelectList(Model.TransactionReferences.Categories, "Value", "Text", Model.Transaction.TransactionsLines[0].CategoryId)
)
public class HomeController : Controller
{
public ActionResult Index()
{
var m = new AccountModel();
m.Transaction = new Transaction();
m.Transaction.TransactionsLines = new List<TransactionsLine>();
m.Transaction.TransactionsLines.Add(new TransactionsLine() { CategoryId = 2 });
m.TransactionReferences = new TransactionReferences();
m.TransactionReferences.Categories = new List<SelectListItem>()
{ new SelectListItem() { Text = "Cat1", Value = "1" },
new SelectListItem() { Text = "Cat2", Value = "2" },
new SelectListItem() { Text = "Cat3", Value = "3" }
};
return View(m);
}
}