dynamically add or remove objects in an asp.net mvc view - asp.net-mvc-2

I have (for example) these two objects:
class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
...
public List<Order> Orders;
}
class Order
{
public int Id { get; set; }
public string Description { get; set; }
...
}
I want one View on which you can edit the Customer data
(that part is already working fine), but I want some kind of
'dynamic grid' to add/remove/update orders on the same page.
How can you do this? With jQuery you can add or remove html controls, but to add or remove
an Order object with it?
Any ideas?
thanks,
Filip

I would recommend you checking out the following blog post from Steve Sanderson which illustrates a nice technique which could be used for editing a variable length list in ASP.NET MVC 2.

Related

What Scaffolding Nuget package for child collections

I am using VS2015 and have MVC5 web app. I want to use scaffolding feature to generate CRUD for my child entities: currently when generating the scaffolding it is NOT creating the views/edit/create for the IEnumerable collection 'Cities'.
I have googled but not found anything. Is there a Nuget package that does what I want. It should allow to add/delete/edit the cities maybe using partial view but should be auto-generated.
code:
public partial class Country
{
public int ID { get; set; }
public string Name { get; set; }
public string Color { get; set; }
public string Location { get; set; }
public virtual IEnumerable<Cities> Cities { get; set; }
}
public partial class Cities
{
public int ID { get; set; }
public string Name { get; set; }
public string TreLocation { get; set; }
public string Geo { get; set; }
}
Do you want an editable list of cities under each country?
Don't use mvc! In the time you spend figuring it out, you can probably learn angular. Which is far better for tis sort of thing.
However, back in the mists of time wise sages wrestled with this problem, for example....
Collection of complex child objects in Asp.Net MVC 3 application?
https://www.donnfelker.com/editable-grid-list-binding-in-mvc2/&ved=2ahUKEwiltefd9cngAhWjQxUIHe45AM4QFjACegQIARAB&usg=AOvVaw2z8iH5hZnObcnIMl3d4cyI

Are there are any libraries that support user content localization in aspnetcore?

The are plenty of resources describing G11n an L10n in aspnetcore including the official docs.
But are there any libraries that simplify the implementation of a user content localization? An example could be a content of a blog post that may be translated into multiple languages. Such a library would use a specific table in SQL for storing/retrieving translation.
Here is the possible use case:
// this object contains content that user can add manually
public class BlogPost
{
// should be localised
public string Content { get; private set; }
}
It seems like we can add a collection of "string Content" in order to solve this issue:
public class LocalizableContent
{
public string CultureInfo { get; private set; }
public string Content { get; private set; }
}
public class BlogPost
{
public ICollection<LocalizableContent> Content { get; private set; }
}
Note: after making a bit of googling I found a related question (but it gives no answers):
Best Practices to localize entities with EF Code first | StackOverflow
Also, it doesn't seem like this library can help:
github.com/damienbod/AspNetCoreLocalization
Any suggestions?
I think you can implement your content localization library without the need to use third parties. Personaly I do something like below:
public class BlogPost {
public int Id { get; set; }
public ICollection<BloPostLocalized> Localizations { get; set; }
}
Create a localized class for BlogPost:
public class BlogPostLocalized {
public int Id { get; set; }
public BlogPostId { get; set; }
public BlogPost { get; set; }
public string Culture { get; set; }
public string Title { get; set; }
public string Content { get; set; }
}
Notice that the main BloPost has no Title nor Content fields, because we will have them defined in the BlogPostLocalized class for neutral and localized cultures.
So each blog post will have multiple localized versions that can be fetched simply from the db as a child of the main post.

Why MVC does autoupdate of EF Model Classes?

I generated Entity Model from my database in my MVC5 App.
When I try to add [DispalyName] to some properties it works fine, but after some time app refreshes this class by itself and removes all my custom code
public partial class Patient
{
public Patient()
{
this.PatientDetails = new HashSet<PatientDetail>();
}
public int PatientID { get; set; }
[DisplayName("Last Name")]
public string LastName { get; set; }
public string FirstName { get; set; }
public virtual ICollection<PatientDetail> PatientDetails { get; set; }
}
Why MVC does it and how to disable that?
I believe since you're using Database first, the entities will get completely re-created every time you refresh, thus you lose your custom attributes.
Also, to go off of Joe's comment, you should make a view model and put your [Display] attributes there and not directly on the entity.

MVC 4 unique field model

I have been Googleing this and had no joy.
How do i add unique to a field in a code first approach in mvc4 .net
public class Religion
{
public int ReligionId { get; set; }
[Required]
[StringLength(200)]
public string Description { get; set; }
}
What would i need to add to description for this to work
you may use remote validation
review that link http://msdn.microsoft.com/en-us/library/gg508808(v=vs.98).aspx
simple and fast

How to create a LINQ to Entities query including both tag class name and text string

My ASP.NET MVC project has some
chapter class
and related
tag class
They have many to many relationship though one tag has multiple chapter and also one chapter has multiple tags on it.
My question is: I want to make a query by using tag.title within this structure but LINQ doesn't allow objects; it only gets variables like string, int ...etc.
I am feeling a little bit stupid but I can't figured it out this basic thing :( (May be I have to give a break to let it be clear in my mind)
My models are:
public partial class Chapter
{
public string Id { get; set; }
public string Subject { get; set; }
public string Content { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
}
public partial class Tag
{
public int? TagId { get; set; }
public string Title { get; set; }
public int CallCount { get; set; }
public virtual ICollection<Chapter> Chapters { get; set; }
}
and also for db:
public class DataContext : DbContext
{
public DbSet<Chapter> Chapters { get; set; }
public DbSet<Tag> Tags { get; set; }
}
so here we go:
var tag = (from t in db.Tags where t.Title.Contains(search_word)).FirstOrDefault();
var chapters = (from c in db.Chapters where (m.Title.Contains(search_word) || m.Tags.Contains(tag)) select c).ToList();
As I explained above; it gives
"you should use string, guid, int... on queries"
error for this query. I think there is a logical failure I made but my brain stopped responding...
Briefly; I want to search by tag names and if you have another approach I will be pleased to listen it..
Any help will be highly appreciated.
Thanks!
You want:
var tag = (from t in db.Tags
where t.Title.Contains(search_word)
select t).FirstOrDefault();
var chapters = (from c in db.Chapters
where m.Title.Contains(search_word)
|| m.Tags.Any(t => t.TagId == tag.TagId)
select c).ToList();