Url.Action handling null values or using javascript clientside values
OK my original post:
Url.Action is throwing error CS1026: ) expected
got answered. However I discovered that one of the objects in the Model that I was relying on for an ID is null. I can't figure out how to rewrite this to make it work.
var url = '<%= Url.Action(
"List",
"PlacementAgreementAgencyPlacementAgreementsPlacement",
new {
Domain = "Placement",
Id = Model.Item.PlacementAgreement.PlacementAgreementId,
agencyPlacementAgreementId = Model.Item.AgencyPlacementAgreementId,
Page = Model.PageNo
}) %>';
I need something like this (which is currently saying too many characters in character literal which I think might be related to single/double quotes.
var url = '<%= Url.Action(
"List",
"PlacementAgreementAgencyPlacementAgreementsPlacement",
new {
Domain = "Placement",
Id = ' + $("#PlacementAgreementId").val() +',
agencyPlacementAgreementId = ViewData.Model.AgencyPlacementAgreement == 开发者_运维技巧null ? 0 : ViewData.Model.AgencyPlacementAgreement.AgencyPlacementAgreementId,
Page = ViewData.Model.PageNo
}
) %>';
Have also tried:
var url = '<%= Url.Action(
"List",
"PlacementAgreementAgencyPlacementAgreementsPlacement",
new {
Domain = "Placement",
Id = %>' + $("#PlacementAgreementId").val() +'<%=,
agencyPlacementAgreementId = ViewData.Model.AgencyPlacementAgreement == null ? 0 : ViewData.Model.AgencyPlacementAgreement.AgencyPlacementAgreementId,
Page = ViewData.Model.PageNo}) %>';
But this mix of javascript and url.action is just confusing me. My issue is that there is no property on my ViewData class that contains the id.. but if I view source on the page I can see that it is being stored client side.
Any ideas? Thanks!
Ended up going back to what was originally there and passing what looks like old school querystring parameters.
var url = '<%= Url.Action("List") %>?page=' + pageNo +
'&agencyplacementagreementid=' + $("#AgencyPlacementAgreementId").val();
So not sure if I was on the completely wrong track before and you're supposed to do it this way - or if there was a way of doing it like my original approach. But it's now working! :)
Jen,
You are mixing client side Javascript with C# server side, not a good mix!
The only way I see you doing this is by putting a placeholder and then do a replace in Javascript, something like:
var url = '<%= Url.Action(
"List",
"PlacementAgreementAgencyPlacementAgreementsPlacement",
new {
Domain = "Placement",
Id = 9999,
agencyPlacementAgreementId = ViewData.Model.AgencyPlacementAgreement == null ? 0 : ViewData.Model.AgencyPlacementAgreement.AgencyPlacementAgreementId,
Page = ViewData.Model.PageNo}) %>';
And then do a replace in Javascript:
var realUrl = url.replace('9999', $("#PlacementAgreementId").val());
精彩评论