I have a custom MapRoute
context.MapRoute("participantByGender",
"Admin/Participants/{id}/{gender}",
new {
controller = "Admin",
action = "Participants",
id = UrlParameter.Optional,
gender = UrlParameter.Optional
}
and i need to have a link which specifies both the id and a default gender. I have this one, which works for choosing the id, but how would i alter this to include the id and the gender in the link?
#Html.ActionLink("Participants", "Participants", new { id = item.termId })
Simply use:
#Html.ActionLink("Participants", "Participants", new { id = item.termId,gender=model.property })
Related
I´m having trouble understand EF.
I have 2 tables in a N-to-N relationship. When I create the model in Visual Studio I get it like the picture below.
Artist is related to Album, and Album is related to Artist.
In the database I have a table to relate both tables.
My question is:
Why can I add an artist with two albums,
var artist = new Artist { FirstName = "Alan", LastName = "Jackson" };
var album1 = new Album { AlbumName = "Drive" };
var album2 = new Album { AlbumName = "Live at Texas Stadium" };
artist.Albums.Add(album1);
artist.Albums.Add(album2);
context.Artists.Add(artist);
But can´t do the other way around?
var artist1 = new Artist { FirstName = "Tobby", LastName = "Keith" };
var artist2 = new Artist { FirstName = "Merle", LastName = "Haggard" };
var album = new Album { AlbumName = "Honkytonk University" };
artist1.Albums.Add(album);
artist2.Albums.Add(album);
context.Albums.Add(album);
context.SaveChanges();
It´s not making sense to me, since both tables in VS model have the Navigation Properties correct, isn´t the same adding to left or right?
In my mind, in the second case EF should add artist1 and artist2 to DB.
Thanks for reading.
FranciscoRC
To expand on Ivan's comment.
With this example:
var artist1 = new Artist { FirstName = "Tobby", LastName = "Keith" };
var artist2 = new Artist { FirstName = "Merle", LastName = "Haggard" };
var album = new Album { AlbumName = "Honkytonk University" };
artist1.Albums.Add(album);
artist2.Albums.Add(album);
context.Albums.Add(album);
Prior to SaveChanges, album.Artists hasn't been set with anything, and all EF has to go on is what you've set on album.
In your case, you've created 2x artist objects, and 1 album. You've associated the album to the artist, but you've only notified EF of the album to be saved. Just as with POCOs just because an artist has an album and an album has an artist, setting the album on an artist doesn't automatically set the artist property on an album. EF does this behind the scenes provided that it sees the related entities on the entity(ies) you add to the context.
So the following should work:
var artist1 = new Artist { FirstName = "Tobby", LastName = "Keith" };
var artist2 = new Artist { FirstName = "Merle", LastName = "Haggard" };
var album = new Album { AlbumName = "Honkytonk University" };
album.Artists.Add(artist1);
album.Artists.Add(artist2);
context.Albums.Add(album);
Prior to SaveChanges being called, artist1.Album will be null because you haven't set anything. After SaveChanges, artist1 & 2 .Album will reference to your new entity because EF resolved the Artists collection on the album and wired up the related references. (Even though Artists 1 & 2 were not added to the context explicitly.)
I am trying to figure out how to seed a complex entity framework code first object. Currently the code I have allows me to insert simple objects but there has to be a way do more complex items, i.e. Address with an AdressType field.
context.AddressTypes.AddOrUpdate(
p => p.Name,
new AddressType { Name = "Original" },
new AddressType { Name = "Shipping" },
new AddressType { Name = "Billing" }
);
context.Addresses.AddOrUpdate(
a => a.Address1,
new Address
{
Address1 = "1234 West Main",
City = "Hannibal",
Region = "MO",
PostalCode = "12345",
Country = "USA",
Type = context.AddressTypes.Where(a=>a.Name=="Original")
});
But while I can "Find" an addresstype by id I can't do a "Where" name equals. Find would work except that I can not guarantee what the id will be with each seed and want to base it on name instead.
Any suggestions on how to do this?
TIA
Solution:
Added a reference System.Linq to the seed class. Then was able to the where clause.
I am trying to create a new story in Rally.
Using: https://rally1.rallydev.com/slm/webservice/1.40/RallyService
Below is the code
var parentStory = rallyService.query(Workspace, Projs["xxx"], true, true, "HierarchicalRequirement", query, "", true, 1, 20).Results[0] as HierarchicalRequirement;
var tag = new Tag[1];
tag[0] = new Tag()
{
Archived = true,
ArchivedSpecified = true,
CreationDate = DateTime.Now,
CreationDateSpecified = true,
Name = tagName,
};
var childStory = new HierarchicalRequirement
{
Name = feedback.FeedBackSubject,
Description = feedback.FeedBackDescription,
Parent = parentStory,
Owner = parentStory.Owner,
Tags = tag
};
return rallyService.create(childStory);
I am getting the following error: Could not set value for Tags: Cannot use type ObjectReference in attribute Tags
Thanks
I usually use the REST endpoints rather than SOAP but I would guess that you'll need to create your tag first before you reference it in the story you are creating. I think the error is due to the fact that the tag being passed in the array doesn't have a ref.
I get error when paging,
my actions:
ProductList(string country, string city, string town, int? pageNumber)
my Route:
routes.MapRoute(
"ProductList",
"myList/{country}/{city}/{town}/{pageNumber}",
new { controller = "Product", action = "ProductList", country="", city="", town= "", pageNumber = UrlParameter.Optional });
Action Link:
Url.Action("myList","Product", new{ country="Finland",city="",town="",pageNumber=2 })
city = 2 ??
I've found a solution as follows:
Url.Action("myList","Product", new{ country="Finland",city="s",town="n",pageNumber=2 })
http:/myList/Finland/s/n/2
ProductList(string country, string city, string town, int? pageNumber)
{
city== "s" ? city = null;
town == "n" ? town= null;
process...
}
to be http:
/myList/Finland/2
/myList/Finland/Helsinki/3
/myList/Finland/town/7
You cannot skip the parameters in that route. You just can't call http://mylist/2 and expect pageNumber to take value 2. Value in first segment is assigned to the first variable in the route. So 2 is assigned to city variable. You have to make sure all parameters before pageNumber gets a non-null value.
Only the last parameter in your route definition can be optional.
Greetings. How can I make access to my article or post by their name?
For example: Like at stackoverflow has access to this question by the name
access to article by article name in ASP.NET MVC2access-to-article-by-article-name-in-asp-net-mvc2
On StackOverflow the name part is completely ignored. It's the id that is important. This works: access to article by article name in ASP.NET MVC2 and links to this question. To generate links that contain the name in the URL you could define the following route:
routes.MapRoute(
"NameIdRoute",
"{controller}/{action}/{id}/{name}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional, name = UrlParameter.Optional },
new { id = #"\d+" }
);
And then:
<%: Html.ActionLink("some text", "action", new { id = Model.Id, name = Model.Name }) %>
Create a route that allows the name to be specified as part of the url. Note that it's probably not actually used in resolving the article as it might not be unique. The id is the bit of information that is actually used to find and display the correct article, but the name is part of the url for context.
routes.MapRoute(
"Question",
"{controller}/{id}/{name}",
new { controller = "questions", action = "show", id = UrlParameter.Optional, name = UrlParameter.Optional },
new { id = "[0-9]+" } // constraint to force id to be numeric
);
Now, when you use Html.ActionLink() and specify the name as a parameter, the route will match and put in the name as a component of the url instead of a query parameter.
<%= Html.ActionLink( "questions", new { id = Model.ID, name = Model.NameForUrl } ) %>
Note that if you have multiple routes that might match you may need to use RouteLink and specify the route by name. Also, order matters.