C# MVC: Can we have partial update on the model? - c#

We have c# model class with following fields:
public class BasicDetailsModel
{
public string Name { get; set; }
public string Email { get; set; }
public string Mobile { get; set; }
}
Once user clicks on save button with (Name="x", Email="x#gmail.com", Mobile=989), these details get saved in database. Now, in case user updates any of these fields i.e. Name to "y", we have to send Email,Mobile too although these have not changed.
I have oversimplified the problem here. We have multiple cases where Models have over 20 fields and we are updating more than 5 models through single save click.
How can we update only what's needed and assure everything else already saved remain as it is i.e. in this case client would only send (Name="y") and Database will have (Name="y", Email="x#gmail.com", Mobile=989).
I don't want to make multiple database calls before saving and want flexibility that user remove value from already entered fields i.e. can change Name from "y" to null.

Related

Entity Framework - Get name of entity based on ID from another table

I'm not sure how to phrase this question correctly but here's an issue I am currently facing in a backend development project.
Suppose I have a table called Users. I create a model called User:
public partial class User
{
public int Id { get; set; }
public string Name { get; set; }
public id JobTitleId { get; set; }
... other fields
}
A user has a Job title which is related to a JobTitles table. The way its structured now is the FE will call an api, lets call it dictionary api which returns a list of objects with the job titles. So, something like this:
[{id: 1, title: 'aa'}, {id: 2, title: 'bb'} ...]
So, based on the jobtitleid that was available, this api will be called and the view will show the title based on the id.
Now, I feel like this is not really efficient since if we have a hundred or thousand job titles, FE will have to parse through each object to match the id.
Is there a better way to display the name of the job title given the ID? I cannot return the job title directly because if the user's job title is not updated but that particular form with the job titles is sent with a PUT request, it will return the name of the job title instead of the ID back to the users table.
Also, the job title will be an autocomplete field on the Frontend which will call an api on the backend when a user inputs the name of the job title, thereby returning the id of that job title back to the Backend when a user is updated with a new job title.
Sorry if Im not clear enough, I can ive more details if required.
have you tried doing something like this?
I am assuming JobTitleId belongs to JobTitles class
public partial class User
{
public int Id { get; set; }
public string Name { get; set; }
public id JobTitleId { get; set; }
[ForeignKey("JobTitleId")]
public JobTitles JobTitles{ get; set; }
}
so as you know classes are object by reference and when you pass the User class to your view, the moment you change the user title from the dropdown it will also change the jobtitleId accordingly

Asp.net MVC 3 - Data Validation

I'm new at ASP.NET MVC web framework. My database is compound of a single model class("Movie"). I need to validate user's from entering existing data, for instance, a database row could be:
Title - "Indiana Jones and the lost Arc"
Price - $10.00
If another user tries to insert into the database the same data above, provide an error message and prevent from submitting the form collection.
First of all, I think that your question need a "program as answer" but I'll try to suggest you from where to start for working with validation. Suppose you have this model:
class Movie {
public Guid Id { get; set; }
[Required(ErrorMessage="Title is required.")]
[Remote("UniqueTitle", "Validation")]
public String Title { get; set; }
[Required(ErrorMessage="Price is required.")]
public float Price { get; set; }
}
You can decorate it for "simple validation" using Data Annotation. I've used a specific attribute, called Remote.
This attribute allow you to define a custom, server-side, logic to validate the model.
Now, you can create a validation controller where check that provided value is not already in use:
class ValidationController : Controller {
private IDbContext db = ...;
public ActionResult UniqueTitle(String title) {
var item = db.Movies.FirstOrDefault(m => m.Title.Equals(title));
return Json(item == null, JsonRequestBehavior.AllowGet);
}
}
Now you are ready to validate your model.
I hope this can help.

Sitecore WFFM - How to trigger a form externally

I have a web forms for marketers, and I need to "run" that webform through code from somewhere else.
Is there a way to load the webform and run its execute action? For example with a regular sitecore item I can get it based on its ID, but I don't seem to be able to find a way to cast a regular sitecore item, to a WFFM item and then execute its save actions (and possibly set its fields).
It sounds like you are trying to insert data into the WFFM database without having to run through the WFFM submit action? Did I get that right?
If so, you could submit form code to the WFFM DB using the following code, and it would show up seamlessly in the report page of that form:
Say you have your list of fields populated in a list of this class:
public class WffmField
{
public string FieldName { get; set; }
public string FieldGuid { get; set; }
public string FieldValue { get; set; }
}
the field guid would be the guid from sitecore:
You can then save to the WFFM database:
// This should be populated with the data you want to send to the WFFM database
var fields = new List<WffmField>();
var wffmDatabaseFields = fields.Select(GetWFFMDatabaseField).ToList();
Sitecore.Forms.Data.DataManager.InsertForm(
formId: new Sitecore.Data.ID("<Form guid here>"),
fields: new AdaptedResultList(wffmDatabaseFields),
sessionID: AnalyticsTracker.SessionId,
data: null);
Hope this helps!

Dropdownlist ASP.NET MVC not updating Model

I have two Model classes like so:
Program:
public class Program
{
public int ProgramId { get; set; }
public string Name { get; set; }
public virtual Playlist chosenPlaylist { get; set; }
public virtual IList<Playlist> Playlists { get; set; }
}
public class Playlist
{
public int PlaylistId { get; set; }
public string Name { get; set; }
public int NumberVotes { get; set; }
public virtual IList<Song> Songs { get; set; }
}
In my Edit Program View, I want to update the chosenPlaylist so I can allow the user to select none or one of the Program's Playlists.
For example:
Program 1:
Playlist 1
Playlist 2
Chosen Playlist: Playlist 1
So the user can then edit and select None (so no playlist), 1 (won't change anything) or 2 and that gets saved to the database.
I've tried to create a dropdownlist in my Controller but it won't update.
Here's what I have in both my GET and POST Edit ActionResults:
ViewBag.chosenId = new SelectList(program.Playlists, "PlaylistId",
"Name", program.chosenPlaylist.PlaylistId);
And in my View:
#Html.DropDownList("PlaylistId", (SelectList)ViewBag.chosenId)
This displays the list fine and pre-selects the chosen Playlist, if there is one (if not, I'll write code for it to default to the first). If there aren't playlists in a Program, that's easy to control.
However, problems:
Doesn't update my model. If Playlist 2 is the chosen one, for example, and I choose P1, it continues to display P2 after the POST event.
I want to include an option in the dropdownlist for it not to pick any value (so, place a NULL in that field). Is that possible?
There are no errors thrown, everything seems to work except for the most important part - updating the database.
Make sure there are no #Html.HiddenFor or similar rendering the same item.
There's 2 things you can do:
Change #Html.DropDownList("PlaylistId", (SelectList)ViewBag.chosenId) into #Html.DropDownList("chosenPlaylist.PlaylistId", (SelectList)ViewBag.chosenId)
Or use the Html.DropDownListFor(m => m.chosenPlaylist.PlaylistId, (SelectList)ViewBag.chosenId)).
When using:
#Html.DropDownList("PlaylistId", (SelectList)ViewBag.chosenId)
you have to rename the PlaylistId select list to something that is not the same as the propertyname that stores the selected Id, else it wont be marked as selected.
(which makes sense now that im typing it, you cant store the selected value into something that has the same as the select list)
Basically saying:
#Html.DropDownList("MySelectListId", (SelectList)ViewBag.chosenId)
will work.
You can look at the comments on this issue at codeplex for more information: http://aspnet.codeplex.com/workitem/4932

ASP.NET MVC3 Intranet Site

I'm struggling with the design aspect of building this web site, its my first website and I'm not sure of the correct direction i need to take this project in. I've posted on this previously but not done a good job of explaining it. So I'll attempt to do so now.
The Site will be used for submitting "Innovation Ideas" from employees. It will have to connect up to an already existing MS Access Database. There will be two tables in this database that it has to communicate with.
The first one Is the InnovationSubmission Table which which looks similar to this :-
ID(Auto Generated),
Short Description,
Long Description,
Name,
Email Address,
Date(From Date.Time.Now()) - on Submission.
Team Name
Area (Area of Business)
The User will use a Web Form(View) to enter the details above, it will be validated, then saved to the back end database. I have this working in a fashion. The issue has started when I have tried to introduce two DropDownlistsFor contorls based on another table which is below :-
AREA A - Team1, Team3, Team5, Team7
AREA B - Team2, Team4, Team6, Team8
This is just sample Data, there are only two different areas in the table, but there will be over 50 teams split across them. I will be looking to have the Teams dropdownList filter on the value in the Area DropDownlist.
In my Models folder I have created a InnovationSubmission Class, that replicates the table in the database, this class is used as a strongly typed data type in the View representing the Submission Form. Its how i Validate the User input and I pass this class to a c# method that sends the data back using ADO.NET.
I'm struggling with how I should be trying to implement the dropdownlists.
Will I need to create a class similar to the InnovationSubmission Class, to represent the Teams/ Area Table?
Is the table at present structured in the best way for this project?
Can I populate both dropdownlists from the 1 table?
How do I relate the Teams & Area Columns?
Any Help would be greatly appreciated!?!
Would this be the correct way to design my View Model :-
public class MyViewModel
{
public int ID { get; set; }
public string shortDesc { get; set; }
public string longDesc { get; set; }
public string Status { get; set; }
public string originator { get; set; }
public string originatorEmail { get; set; }
public IEnumerable<Area> area { get; set; }
public IEnumerable<Team> team { get; set; }
}
public class Team
{
public string teamName { get; set; }
}
public class Area
{
public string area { get; set; }
}
You seem to be talking about cascading dropdown lists where the values of the second one update based on the selection made in the first one. I have illustrated an example of how this could be achieved in this post. The idea is that you subscribe to the .change event of the first dropdownlist and then trigger an AJAX request to a controller action passing the selected value in which you wold query the database and return a list of possible options.

Categories

Resources