Programming Strategy for ASP.NET MVC 5 - Updating a Child or Related Records on Same Page - asp.net

I am struggling a bit with strategies that allow for updating child records related to a parent record in MVC in the same view. Take for instance model classes described below.
public class Person {
public Person() {}
int personId{get;set;}
string personName{get;set}
public virtual ICollection<Phone> Phones { get; set; }
}
public class Phone {
public Phone() {}
public int PhoneId {get;set;}
public string PhoneType {get;set;}
public string Number {get;set;}
public string Ext {get;set;}
}
Providing a single page view allows for Creating,Updating,Deleting a person easily.
Adding actions that jump to views designed specifically for the phone records and then return the user to the person record is also straightforward.
What is the best strategy for displaying the related phone records for the person in the same view as the person information and allowing for the CRUD operations to be used against only the phone entities in the same view?
If the answer is that AJAX is the best approach then that is perfectly acceptable. I am looking for feedback from the community that describes what individuals have used in this type scenario in their projects that have this type of challenge.
There was one approach listed here:
http://www.codeproject.com/Articles/786085/ASP-NET-MVC-List-Editor-with-Bootstrap-Modals
That had a decent if somewhat cumbersome approach.
I have to believe that this is a much simpler problem and that I am quite literally missing the obvious.

I've done in-line editing by displaying the current phone information and having hidden inputs. When the user clicks the edit button, I toggle visibility to hide the display and show the inputs.
I've also done similar to the article. Have a modal popup to edit phone numbers, after saving the changes and closing the modal refresh the partial with the phone number list.
In either scenario I'm just submitting an ajax request and return a partial that I use to replace the div that contained the original information.
The modal dialog seems to be little more "modern".

Related

How to achieve below requirement by using Dev Express controls

Note: I only need to use Dev Express controls and I am using windows forms with dotnet framework 3.5
Below are my requirements
I need to display multiple check-in check-out entries with time as displayed in the screen shot and also reason specified below each check-in checkout entry.
I need to display a edit icon for each row when user clicks on the edit icon a time picker need to populate and user can modify the time and the list box time need to modify
I need to display a delete icon at the last entry when user deletes the last entry the delete icon should display to the above entry.
I have CheckinCheckOut button which need to add the data to list box in the same format which i have displayed the image
Which control should i use to complete all the above requirements?
If it is list box control for the above requirement can you please provide me a sample which satisfies all the conditions of the requirement.
Please provide a sample with the below requirement
Looks like this could be done with the GridControl. Checkin/Checkout statuses can be a column, the check-in/check-out time would be a second column and the delete/edit icon can be a third, unbound column. The reason (initial check-in, meal break, check-out etc...) could be done by binding the GridView's Row Preview Section to another field in the data source.
For instance, a simple data source such as:
public class EmployeeCheckIn
{
public enum CheckInTypes { CheckOut, CheckIn };
public CheckInTypes CheckInType { get; set; }
public DateTime CheckInTime { get; set; }
public string CheckInReason { get; set; }
}
Your GridControl could be bound to a List instance, having column 1 bound to CheckInType, column 2 bound to CheckInTime and the preview row bound to CheckInReason.
The unbound column with the edit button can make use a RepositoryItemButtonEdit control in which the text box is hidden, the ButtonKind property is set to Glyph and an "edit" image is supplied to draw the icon. Handle its ButtonClick event to invoke your edit mode.

How do you completely hide an element from a user based on their ASP.NET Identity Role?

Goal: Hide certain elements like page links from users based on their role in ASP.NET Identity.
What I have tried: [Authorize(Roles = IdentityHelper.Administrator)]
(This does restrict access to certain elements if you put the annotation over them, like pages, but it doesn't hide the element itself. I want it to do both.) Its not entirely critical that I hide these elements from the user since they're already restricted, but it would make my website look better to the users.
(IdentityHelper is just a helper class that sets up all the details about the administrator role)
Code Example:
//Restricts access, which is good, but does not completely hide elements from user.
[Authorize(Roles = IdentityHelper.Administrator)]
public async Task <IActionResult> Edit(int id)
{
//get pixel art with corrosponding id
PixelArt p = await PixelDBManager.GetSinglePixelAsync(id, _context);
//pass pixel art to view
return View(p);
}
Should I perhaps switch to Claims or Policy based identity instead of Roles or can I stick with Roles to solve this particular problem?
You can use User.IsInRole() in your Razor template.
#if(User.IsInRole(IdentityHelper.Administrator))
{
<h1>I'm an administrator</h1>
}

Why should I specify a data context class when scaffolding a view?

When adding a new view to an ASP.NET MVC 5 project using the Add View dialog pictured below, I'm invited to choose a template and a model class, which allows me to quickly generate a form for creating new instances of the model or a view that displays the model's properties. But why should the view care what the data context class is? In my project, whether or not I specify the data context class, the same view is generated, but I'm guessing there's a scenario where it would make a difference. What might that be?
If you refer to an existing DbContext then the wizard will insert public DbSet<Employee> Employee { get; set; } (if it doesn't already exist) in your DbContext derived class . Looks like Visual Studio doing some of the leg work.

ScaffoldColumn(false) not working because it is a FK

I am using ASP.NET Dynamic Data. The page dynamically creates a List, Edit, Details pages to display the records stored in the table in the DB.
I have the Sub Contractors in the table but I want its visibility set to false on screen.
I've tried
[HideColumnIn(PageTemplate.List)]
[Display(Name = "SubContractor Id", Order = 70)]
public object SubContractorId { get; set; }
ALSO:
[ReadOnlyColumnIn(PageTemplate.List)]
But no joy. Iv made these changes in the database.cs file.
Anyone know how can I make it disappear or remove the hyperlink from it?
EDIT:
[ScaffoldColumn(false)] usually works but because SubContractorId is a FK it seems to not take effect.
Consider the possibility of using Custom pages (List and other) for your Dynamic Data site which allows you hide specific fields.
For more information: How to: Customize the Layout of an Individual Table By Using a Custom Page Template

Is there an Attribute that hides a property from being bound to a gridview in asp.net?

I have an object that I set as the datasource for a gridview - this works fine, I get a nice table on the page with a column for each public property.
But - I always want to hide one of the columns (but still need it available as a public property.
I'm using a clunky hide-column-on-row-created fix for now, but am looking for a better solution, like an attribute applied to the property to hide it from databinding.
Apparently this exists in winforms:
[Browsable(false)] // this stops Type from showing up in databound controls
public string Type { get; set; }
public string Description { get; set; }
Can anyone suggest a similar solution for ASP.NET?
Update:
I marked Rex M's answer as correct, because it answers the question, but if anyone else is interested in how to do this:
What eventually worked for me was to mark the property corresponding to the column I wanted to hide as internal instead of public.
Looking at the reflected code for GridView.CreateAutoGeneratedColumns(PagedDataSource dataSource), it appears there are not any checks for attributes when it is scraping the datasource for properties. So, apparently not.

Resources