ASP.NET MVC WEB API - c#

New to MVC. I did the tutorial # [http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/build-a-single-page-application-(spa)-with-aspnet-web-api-and-angularjs] and from this you produce a question and answer website. If I wanted to maintain progress i.e. keep a count of the number of questions correctly answered, do I need to calculate this value from retrieving the db.TriviaAnswers object or do I need to add a Count property to the TriviaAnswer class or do I need a separate variable then how do I maintain state between requests? Like ViewBag is not available in the
public async Task<IHttpActionResult> Post(TriviaAnswer answer){...}
method.
OPTION 1 as suggested below:
namespace GeekQuiz.Models
{
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Newtonsoft.Json;
public class TriviaResults
{
[Required, Key, Column(Order=1)]
public string UserId { get; set; }
[Required, Key, Column(Order=0)]
public virtual int QuestionId { get; set; }
}
}
This code throws an InvalidOperationException in the method:
private async Task<TriviaQuestion> NextQuestionAsync(string userId)
on the first line of code.
lastQuestionId = ...

I went over this tutorial a few months ago.
option 1: If you want to track progress I assume you mean progress per user, then I would advice you to add a table to the db which states saves the users ids and the ids of questions which were correctly answered - that's in case you want to save this as a persistent data and per user.
option 2: If you want the same thing, save the data per user but only for this session, you can save the data in the session variable as a dictionary<userid, list<questionid>>.
One thing you should notice is that those question repeat in an endless loop, so you might want to change that.
In both options when you need to know the count u can just go to the table or dictionary and get the number of correct answers.
I hope that answers your question.
To use the session var:
Session["name"] = value;
Session.Remove("name");

Related

What is the most efficient method of detecting whether an object's content has changed and update the object? C#

The Problem
Often controller classes (MVC, MVVM, MVP) that manipulate views have access to a multitude of services. The purpose of one of these services is to update the controller classes with data that is pertinent to the controller.
Take this class for example:
public sealed class RestaurantInformation
{
public IEnumerable<Networking.Models.ServerModels.Restaurant> NearestRestaurants { get; internal set; }
public IEnumerable<Networking.Models.ServerModels.Restaurant> NewestRestaurants { get; internal set; }
public IEnumerable<Networking.Models.ServerModels.Category> Categories { get; internal set; }
}
Whenever the service receives updated information from the network regarding Categories, NewestRestaurants or NearestRestaurants, it packages all of the data into an object which has the class type RestaurantInformation and sends it to the controller class to pass to the view.
I decided to put my C grade in my art GCSE to good use and construct diagrams to aid your understanding of my problem. (Apologies for what you are about to see.)
As you can now see, the flow is as follows:
The view loads which in turn calls the RestaurantViewControl.
The RestaurantViewControl then calls the RestaurantService to retrieve the new categories from the API.
The API returns the new categories to the RestaurantService. (You can see here the restaurant service now has a list that contains B).
The RestaurantService then notify's the RestaurantViewControl using the class above with the new list of categories!
We must now update the list of categories in the RestaurantViewControl in the most efficient way possible with the new items.
I am currently clearing the categories list and then replacing all the values with the new list. The two things I want to know are:
What is the most efficient way to detect a change in the Categories List object?
What is the most efficient way to update the categories list which may still contain objects that are completely valid in that list.
Seems like you have a straight forward issue. You will have a services layer that calls when you show the restaurant list page.
So your collectionView/listView just displays the list of items in the view cell based on that data. One example https://almirvuk.blogspot.com/2019/07/lets-play-with-collectionview-layouts.html?m=1
Usually you’ll just do a check for changes on the first time you visit the page, pull to refresh, or if you set up caching-after a set time when the cache expires.

Validation of object within a list in Parallel C#

I'm implementing an import functionality, wherein I need to read the data from the user uploaded CSV or Excel file and run a few validations and sanitize the data before writing the data to DB.
I'm able to get the data from the file to a list of objects, with the following structure:
public class Order
{
public string Sku { get; set; }
public decimal Cost { get; set; }
public DateTime OrderFulfillmentStartDate { get; set; }
public DateTime OrderFulfillmentEndDate { get; set; }
public string ValidationErrors{ get; set; }
}
Following are the validations that need to happen within the objects in the list, a few examples below:
No two orders with the same SKU and OrderFulfillmentStartDate, OrderFulfillmentEndDate are allowed.
No two orders with the same SKU and overlapping OrderFulfillmentStartDate, OrderFulfillmentEndDate are allowed.
etc.
The way I implemented it:
During the first encounter of a distinct record (passing all the validations AND "ValidationErrors" == string.empty), I'm adding the record to a temp list.
During the next iteration, I'm validating the currently processing record with the record present in the temp list, if the validation fails, I'm populating the "ValidationErrors" field and adding the temp list.
E.g:
Now coming to the crux of the issue:
The size of the data can be around One Million rows.
When I implemented the validations sequentially using foreach loop, the validation process is taking more than 8 hours.
Having said that, I believe doing the validation in parallel would cut down the time needed drastically.
I did try implementing the logic using Parallel.ForEach and Partitioner concept. The processing did quicken up, but I'm not sure, how I can keep a temp list that can be used/updated by multiple threads in the ForEach loop, during the validation.
Is there is a better or quicker approach to achieve what I'm trying to do here? Please do let me know about it.
Thank You!

How can I validate a List/Array Count in an MVC Model using Data Annotations?

How would one go about validating a nested List of Objects in an MVC Model?
I have an "Item" object which has an attached List of Revenue Entries:
public class ItemModel
{
public int ItemID { get; set; }
public virtual List<RevenueEntryModel> Revenues { get; set;}
}
This list can be edited on the page dynamically, and each item is validated individually using it's own model - "RevenueEntryModel". However, I want to be able to restrict users from entering Items without any Revenues.
Is it possible to validate whether this Revenues list is empty using Data Annotations? I'm already using Foolproof but I'm pretty sure it doesn't include this functionality.
There's a previous answer that will help you here. It's a thorough answer, but basically you need to use custom validation attributes:
You can apply your own logic that checks the number of items in the Revenues collection.
Apply a class level validation attribute to the ItemModel class. You could use System.ComponentModel.DataAnnotations.CustomValidationAttribute for this.
This points to a custom method you would create.
The attribute construct would look something like this:
[CustomValidation(typeof (MyClassWhereMethodIsLocated), "MyStaticMethodName")]
Checkout this blog for additional details

DB Design, To normalize or not?

Using MS visual studio 2012, Asp.net C# MVC 4, Entity Framework, NopCommerce(cms).
Hi guys I have Database Design Query, its actual confused me, normally I have no problems with DBs.
However since transitioning over to the Code First approach I asked my self this question...
I am Creating a new plugin for my NopCommerce CMS website, This plugin shall be a ImageGallery Plugin.
I would like the Data layer to store an
ID,
Name,
LargeImg
SmallImg,
Urlimg
But I also want to realize the functionality of this Plugin, The user should be able to upload any image and then Associate this image to a section of there choosing, What i mean by this is Img to a blog post, or Img to news post, Img to a product post OR all of the 3.
Now these three examples are the only ones i can think of, but as you have guessed this may change depending on additional content types.
Now Instantly I thought, Easy we simply create a field called.....Type? or ContentType?
this field will then store the "type" of image association, whether it is a Blog, news or product item.
At which point i thought of "but what if an image has multiple associations?
To which brings me to the question, in this situation, Should i:
Create Separate Columns for each "content Type" (non Normalized)
Create 1 Column Called "content-type" (normalized)
Create a completely Separate table Called "content-types" and use relation
For some reason I'm stuck, I don't normally draw a blank on DB design and implementation.
The code below is my Domain Class in my plugin, i went for number 2 but im not sure to continue down this road.
using Nop.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Hroc.Plugin.Misc.ImageGallery.Domain
{
public class ImageItem : BaseEntity
{
public virtual int ImageID { get; set; }
public virtual string Name { get; set; }
public virtual byte[] Largeimg { get; set; }
public virtual byte[] Smallimg { get; set; }
public virtual string URLimg { get; set; }
public virtual string Typeimg { get; set; }
public virtual int LinkID { get; set; }
}
}
hopefully you guys can point out the correct way to implement this, thanks!
With everything there is a trade-off
Benefits of normalization in your case:
Extensibility - adding another content type requires no structure/class change
Smaller tables (with variable-length data the difference may not be significant)
Drawbacks:
Querying - if you need to pull multiple types in one query you'll need to de-normalize.
Integrity Overhead - possibility of orphaned data if not managed properly
If I were designing this feature I would go with Option 3 - normalizing the content types has other advantages such as being able to use that table for drop-down lists.

What is the most standard way to persist data between MVC views?

I'm creating a system using ASP.NET MVC for filling out online job applications. It's really just an experiment to familiarize myself with the MVC framework. I'm definitely not a security expert so I just wanted to get some advice here since this web app will be dealing with sensitive information.
The system has a wizard-like set of views. You go from one view to the next filling out the relevant information until the end when you finally submit it.
Each job application is stored in the database with a GUID primary key. Right now I am simply storing that GUID in a hidden field in the view of each page. This makes it easy to update the model in the controller like so:
[HttpPost]
public ActionResult ExampleAction(FormCollection formValues)
{
Guid appId = new Guid(Request.Form["ModelId"]); // the GUID stored in the hidden field
ExampleModel example = db.Entities.Single(e => e.ModelId == appId);
UpdateModel(example, formValues);
db.SaveChanges();
return RedirectToAction("ExampleAction2", new { appId = appId.ToString() });
}
I know this is not secure because anyone with any kind of development experience knows you can edit the values of hidden fields in any browser's developer tools. So what's the recommended way to securely get the same results?
You should store it in the Session object. That way, you can call it at anytime from anywhere and it will never display on any of your views.
The doc: http://msdn.microsoft.com/en-us/library/ms178581.aspx
I know this has been answered, just for interest sake I want to leave another option. How you could go about it is parsing the information you get from the first view as a JSON string that you can store in a cookie and then later serialize it when you need it.
This is asuming that the information is safe to store client side. Otherwise it could be an encrypted cookie I suppose. This would address the concerns for cloud computing and load balancers etc.
Why not post to each step instead of using RedirectToAction? That way you can create a model that contains each step.. for example:
class JobApplicationViewModel
{
public Guid ApplicationGuid { get; set; }
public StepOne Step1 { get; set; }
public StepTwo Step2 { get; set; }
public int CurrentStep { get; set; }
}
The step 1 view can post to step 2.. for example:
[HttpPost]
public ViewResult StepTwo(JobApplicationViewModel viewModel)
{
ServiceLayer.Update(viewModel);
return View(viewModel);
}
Then, the StepTwo view posts to StepThree, StepThree posts to Step4, etc. This makes sense logically, as you're only allowing people to get to any step beyond step 1 by posting the form.

Categories