Implementing a model with dynamic columns - c#

I am trying to implement one model with dynamic columns.
My model:
public partial class AccountViewModel : DbContext
{
public ACCOUNT Accountdt { get; set; }
}
My controller code returning index.cshtml:
// To display grid in Kendo UI
public ActionResult Accounts_Read([DataSourceRequest]DataSourceRequest request)
{
Json(GetAccounts().ToDataSourceResult(request,ModelState));
}
//To extract data from linq
private IEnumerable<Object> GetAccounts()
{
var database = new Entities();
var load = database.ACCOUNTs.AsQueryable();
AccountViewModel avm = new AccountViewModel();
return load.Select(account => avm.Accountdt);
}
I am not able to see dataset in the result datagrid. Early it is working fine when columns are declare in the model.
Here is my code that's working fine:
public partial class AccountViewModel : DbContext
{
public string Id { get; set; }
}
// To display grid in Kendo UI
public ActionResult Accounts_Read([DataSourceRequest]DataSourceRequest request)
{
Json(GetAccounts().ToDataSourceResult(request, ModelState));
}
private IEnumerable<Object> GetAccounts()
{
var database = new Entities();
var load = database.ACCOUNTs.AsQueryable();
return load.Select(account => AccountViewModel {
id = account.id;
});
}

Change following code,
return load.Select(account => avm.Accountdt);
as follows,
return load.Select(account => new AccountViewModel {
Accountdt = account
});

Related

Creating dropdownlist from database with Entity Framework Core in an ASP.NET Core 5 MVC application

In my app I'm trying to populate a drop-down menu by taking data from the db with EF Core, but unfortunately I've been trying it for two days without success.
I have tried to do this as recommended in this post, but I keep getting errors.
More specifically the error I'm facing now is
'IEnumerable' does not contain a definition for 'Years' and no accessible extension method 'Years' accepting a first argument of type 'IEnumerable' could be found
This error comes from the view:
#Html.DropDownList(m => m.Years, Model.Years, "-- Select year --")
Here is my model class:
public partial class Bdgfixmonth
{
public int Counter { get; set; }
public int Byear { get; set; }
//
public IEnumerable<SelectListItem> Years { get; set; }
public string Bbudget { get; set; }
public int Bmonth { get; set; }
public string Blongmonth { get; set; }
public int Closed { get; set; }
public string Current { get; set; }
}
And here is my controller for the GET action method Index:
private readonly salesContext _context;
public IEnumerable<SelectListItem> Years { get; set; }
public bdgfixmonthController(salesContext context)
{
_context = context;
}
public async Task<IActionResult> Index()
{
var bdgfixmonths = await _context.Bdgfixmonths.ToListAsync();
IEnumerable<SelectListItem> GetAllYears()
{
IEnumerable<SelectListItem> list = _context.Bdgfixmonths
.AsEnumerable()
.Select(s => new SelectListItem
{
Selected = false,
Text = s.Byear.ToString(),
Value = s.Byear.ToString()
});
return list;
}
Years = GetAllYears();
return View(bdgfixmonths);
}
The goal here is to populate the drop-down menu with the data of the db, and then make a query based on the selected value, so that only the relevant values are shown (in this case based on the year).
Any help would be very appreciated, thanks.
Since you're returning a model that contains a list of Bdgfixmonths, it's probably better to create a view model that looks like this:
public class BdgfixmonthsViewModel
{
public List<Bdgfixmonth> Bdgfixmonths { get; set; }
public IEnumerable<SelectListItem> Years { get; set; }
}
Then, your Action Method will change to:
private readonly salesContext _context;
public bdgfixmonthController(salesContext context)
{
_context = context;
}
public async Task<IActionResult> Index()
{
var vm = new BdgfixmonthsViewModel();
var bdgfixmonths = await _context.Bdgfixmonths.ToListAsync();
IEnumerable<SelectListItem> GetAllYears()
{
IEnumerable<SelectListItem> list = _context.Bdgfixmonths
.AsEnumerable()
.Select(s => new SelectListItem
{
Selected = false,
Text = s.Byear.ToString(),
Value = s.Byear.ToString()
});
return list;
}
vm.Years = GetAllYears();
vm.Bdgfixmonths = bdgfixmonths;
return View(vm);
}
Your ViewModel will have the two collections which can then be used in the view. You will need to update the View to reference the Bdgfixmonth properties like Bdgfixmonths.Bbudget for example. Also, the model will change to something like:
#model IEnumerable<Bdgfixmonth>
becomes
#model BdgfixmonthsViewModel
And then any iteration code will change to:
#foreach (var item in Model.Bdgfixmonths)
{
item.Bbudget
}

How to get specific field(s) from database?

public class UserController : ApiController
{
UserSampleEntities entities = new UserSampleEntities();
// GET api/<controller>
[Route("api/User")]
public IEnumerable<user> Get()
{
{
return entities.users;
}
}
}
This returns the json with all the entries in the database with all its properties. How do I filter such that I can obtain a json for only specific properties?
Create a new class that represents a user using only the properties you want to expose from "api/User":
public class UserDto
{
public int Foo { get; set; }
public string Bar { get; set; }
// add the properties you need here
}
Rewrite your API action to this:
[Route("api/User")]
public IEnumerable<UserDto> Get()
{
return entities.users
.Select(u => new UserDto
{
Foo = u.Foo,
Bar = u.Bar,
// map the properties you need here
})
.ToArray();
}

How to retrieve an object property inside a view

I have an index method in a controller which looks like this :
public ActionResult Index()
{
var object = _ObjectService.GetAll();
return View(object);
}
Which give me a list of object with those properties :
public class Object : EntityWithNameAndId
{
public virtual Site Site { get; set; }
public virtual List<User> Users { get; set; }
public virtual List<Planning> Plannings { get; set; }
public virtual Guid IdPilote { get; set; }
}
Now in my Index() view, i want to get the User who's related to the IdPilote id and display its name.
I tried something like this, thanks to this topic ASP.Net MVC: Calling a method from a view :
#model List<MyClass.Models.Promotion>
#foreach (var item in Model)
{
<td>#item.Site.Name</td>
#{
var id = item.IdPilote;
//Here Interface and Service are folders
var user = MyDAL.Interface.Service.IUserService.Get(id);
}
<td>
//This is where i try to display my User name,
//that i get dynamically using the idPilote for each User in list
</td>
}
But Get(id) is not recognize as a valid method..
public interface IUserService : IDisposable
{
User Get(Guid id);
}
public class UserService : IUserService
{
private MyContext context;
public UserService(MyContext context)
{
this.context = context;
}
public User Get(Guid id)
{
return context.User.Where(w => w.Id == id).SingleOrDefault();
}
}
So what's the best way to get my User object inside my view, since i only get an Id ?
Should i create a new list, using the first one, in my Index method (where i can call IUserInterface.Get()) or is there a better way to do it ?
Make it worked by creating a new list and a specific ViewModel, as suggested :
public class IndexObjectViewModel : EntityWithNameAndId
{
public virtual Site Site { get; set; }
public virtual List<User> Users { get; set; }
public virtual List<Planning> Plannings { get; set; }
//To store User instead of its Id
public virtual User Pilote { get; set; }
}
Now Index() looks like this :
public ActionResult Index()
{
var objects = _IObjectService.GetAll();
ViewBag.NotPromoExist = false;
var indexObj = new List<IndexObjectViewModel>();
foreach (var p in objects)
{
var indexModel = new IndexObjectViewModel();
indexModel.Id = p.Id;
indexModel.Name = p.Name;
indexModel.Site = p.Site;
indexModel.Users = p.Users;
indexModel.Plannings = p.Plannings;
indexModel.Pilote = _IUserService.Get(p.IdPilote);
indexObj.Add(indexModel);
}
return View(indexObj);
}
Everything is done in the controller now. Not sure if it's the best way to do it though..

Return ViewModel instead of Model

The following method returns a IEnumerable MODEL called PROF.
Instead of using the model, I want to use a viewModel which contains the same fields as the Model PROF. How can i make changes to the following function to return my VIEWMODEL(which is called MyProfViewModel ) instead of the model (which is called PROF ).
public async Task<ActionResult> Index()
{
var cp= db.PROF.Include(c => c.ACCOUNTS);
return View(await cp.ToListAsync());
}
ViewModel
public class MyProfViewModel
{
public int myprofID { get; set; }
public string myprofDes{ get; set; }
}
Model
public class PROF
{
public int ID{ get; set; }
public string DESCRIPTION { get; set; }
}
Joel's answer is pretty much it. But since you mentioned you are starting I will provide a more deltailed answer to make more clear how to use.
First you define a conversion, in this case I put it in the ViewModel, but you can put it somewhere else that would make more sense in your project:
public class MyProfViewModel
{
public int myprofID { get; set; }
public string myprofDes { get; set; }
public static MyProfViewModel FromModel(PROF model)
{
var viewModel = new MyProfViewModel()
{
myprofID = model.ID,
myprofDes = model.DESCRIPTION
};
return viewModel;
}
}
Then you just apply the conversion before returning:
public async Task<ActionResult> Index()
{
var cp = PROF.Include(c => c.ACCOUNTS);
var models = await cp.ToListAsync();
var viewModels = models.Select(MyProfViewModel.FromModel);
return View(viewModels);
}
By the way, you can make the tranformation to models as well by adding this to your ViewModel class:
public PROF ToModel()
{
return new PROF()
{
ID = this.myprofID,
DESCRIPTION = this.myprofDes
};
}
Hope this helps!
Add a select statement which maps one type to another:
db.PROF.Include(c => c.ACCOUNTS)
.Select(x=> new MyProfViewModel(){myprofID = x.ID, ...});
Depending on if this select statement is supported by your ORM layer (Entity Framework?), I would do it before, or after .ListAsync();
Pros of before: could lead to better optimized SQL queries.

How to map users identity and Auditable properties in view model

This is my view model.
public class ProductViewModel
{
public Guid Id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public bool IsAvailable { get; set; }
}
When form is posted from client the form is submitted to this Controller
public async Task<IHttpActionResult> AddProduct(ProductViewModel productViewModel)
{
await ServiceInstances.PostAsync("product/add", productViewModel);
return Ok();
}
Then this controller submit the form to the API controller
Which is on my separate Project.
[HttpPost]
[Route("add")]
public IHttpActionResult AddProduct(ProductViewModel model)
{
_productService.AddProduct(model.UserServiceDetails());
return Ok();
}
Extension UserServiceDetails Where i get the Login User Info
public static UserServiceDetailModel<T> UserServiceDetails<T>(this T model)
{
var serviceRequestModel = new ServiceRequestModel<T>()
{
Model = model,
LoginInfo = HttpContext.Current.User.Identity.GetUserLoginInfo();
};
}
AddProductService:
public void AddProduct(UserServiceDetailModel<ProductViewModel> serviceRequestModel)
{
var repo = _genericUnitOfWork.GetRepository<Product, Guid>();
var mapped = _mapper.Map<ProductViewModel, Product>(serviceRequestModel.Model);
mapped.Id = Guid.NewGuid();
mapped.CreatedDate = GeneralService.CurrentDate();
mapped.CreatedById = serviceRequestModel.LoginInfo.UserId;
repo.Add(mapped);
_genericUnitOfWork.SaveChanges();
}
Now my question is Is there any way to assign the value to this field CreatedDate and CreatedById before posting it to service?
Reduce these logic to mapper:
mapped.CreatedDate = GeneralService.CurrentDate();
mapped.CreatedById = serviceRequestModel.LoginInfo.UserId;
Or is there any way that those field gets mapped to Product when
var mapped = _mapper.Map<ProductViewModel, Product>(serviceRequestModel.Model);
Sometime i may have the List<T> on view-model and there i have to add this field using the loop.
So this same mapping may get repeated over and over on Add Method Or Update.
In some entity i have to assign the ModifiedDate and ModifiedById also.
My Mapper Configuration:
public class ProductMapper : Profile
{
public ProductMapper()
{
CreateMap<ProductViewModel, Product>();
}
}
I cannot add the Enitity as IAuditableEntity and Overrride in ApplicationDbContext because my DbContext is in separate Project and i donot have access to Identity there.

Categories