|
Better start writing some code then, you're going to need a lot of it
|
|
|
|
|
at System.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)
at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
at System.Collections.Generic.Dictionary`2.ContainsKey(TKey key)
at System.Collections.Generic.Dictionary`2.KeyCollection.System.Collections.Generic.ICollection<TKey>.Contains(TKey item)
at DotNetAgent.Trackers.ComponentCountInfo.RecordExternalComponent(String componentName)
at DotNetAgent.Trackers.DefaultTracker.Finish()
at DotNetAgent.Trackers.db.DbCommandTracker.Finish()
at DotNetAgent.Trackers.DefaultTracker.Exit()
at DotNetAgent.Sequences.Sequence.FinishTracker(ITracker tracker, IAgent agent)
at DotNetAgent.Instrumenter.Factories.Web.CallHandlerExecutionStepTrackerFactory.FinishTracker(Object dummy)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously
|
|
|
|
|
We can't see your screen. We can't access your computer. We can't read your mind.
Even if we had the error message, which you managed to avoid posting, we don't have access to your code. Without seeing your code, we can't tell you what the problem is.
All we can tell you is that part of your code is using a Dictionary<TKey, TValue> incorrectly. If I had to guess, I'd say you're using it from multiple threads without proper synchronization, which is corrupting the internal state. If that's the case, then I'd suggest using a ConcurrentDictionary<TKey, TValue>[^] instead.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hello, I wrote my first API today and I used AutoMapper. I have a simple clarification question about the bolded line of code here:
public IEnumerable<MoviesDto> GetMovies()
{
return _context.Movies.ToList().Select(Mapper.Map<Movie, MoviesDto>);
}
I am aware that Mapper.Map is an AutoMapper function which is mapping my domain model to my Data Transfer Object (I wrote all of this code from scratch earlier), but I'm slightly confused on this syntax...
Is this basically pulling all of the data in my Movies table out of the database and then taking it and inserting the values into my DTO's properties and then returning those Dto properties to the client all in one step??? I'm guessing that is what it is doing but I'm not 100% sure. Sorry for the ignorance, I've only been coding since May but am excited to have made my first API!
|
|
|
|
|
The .ToList() call pulls all of the data from your Movies table into memory.
The .Select(...) call is part of Language-Integrated Query (LINQ)[^]. It's a projection operation[^] which converts a sequence of values of one type into a sequence of values of a different type, by calling a user-defined function for each item in the source sequence. In this instance, the function it calls is Mapper.Map<Movie, MoviesDto> , which is the AutoMapper function to translate your data class to your DTO class.
It's almost equivalent to something like this:
List<Movie> movies = new List<Movie>();
foreach (Movie movie in _context.Movies)
{
movies.Add(movie);
}
List<MoviesDto> result = new List<MoviesDto>();
foreach (Movie movie in movies)
{
MoviesDto dto = Mapper.Map<Movie, MoviesDto>(movie);
result.Add(dto);
}
return result;
The main difference is that LINQ is lazily evaluated. The Select projection isn't processed until the returned sequence is enumerated, and the items are processed as they are requested, rather than being stored in a list.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
How to use two dropdownlist to select the province and the city?
Thanks.
|
|
|
|
|
|
That's called a cascading dropdown, so google "asp.net cascading dropdown" and you'll find various examples.
|
|
|
|
|
Hi
I am new in DROOLs pls provide me some sample how Drools implemented with MVC Dot Net
Thanks
|
|
|
|
|
All someone is going to do is google "asp.net drools getting started" and post come links from the results, so you might as well just do the search yourself.
|
|
|
|
|
I've spent many, many hours toiling through learning ASP.NET MVC. The good news is, I actually understand most of it. However, there's one thing I'm not making a connection on.
I have a simple view called Customers which just shows a table with customer names and their membership types. I discovered that in the CustomerController, if I do not put this line of code in (in bold), my view cannot access the Membershiptype.Name field:
public ViewResult Index()
{
var customers = _context.Customers.Include(c => c.MembershipType).ToList();
return View(customers);
}
However, I don't have to put a specific Include() in there to access customer.Name. See Customer View code below:
@foreach (var customer in Model)
{
<tr>
<td>@Html.ActionLink(customer.Name, "Details", "Customers", new {id = customer.Id}, null)</td>
<td>@customer.MembershipType.Name</td>
</tr>
}
In this example, I had to add the
Include(c => c.MembershipType). into the chain to access customer.MembershipType.Name from within the View, but there is not a
Include(c => c.Name) needed in order to access customer.Name. Why is this? What exactly is this Include method doing? Thanks.
|
|
|
|
|
This isn't an MVC question but an Entity Framework one (I assume _context is an Entity Framework database context).
If you have data in two tables like
MembershipType
MembershipTypeID, Type
1, Basic
2, Full
Customer
CustomerID, Name, MembershipTypeID
1, John, 1
2, Dave, 1
These two tables relate on the MemebrshipTypeID, but they are part of different entities (each table is an entity). In EF when you ask for Customer entities;
_content.Customers.FirstOrDefault().Name
all it is doing is loading the data from the Customer table so you can only reference things in that table. However as the tables are related, your Customer class will have a MembershipType property too that will hold the data from MembershipType. By default this data isn't loaded though, so this
_content.Customers.FirstOrDefault().MembeshipType
will be null, so this
_content.Customers.FirstOrDefault().MembeshipType.Type
will throw a null reference extension. When accessing data you can tell EF to load related tables too by using Include
_content.Customers.Include("MembeshipType").FirstOrDefault().MembeshipType.Type
this will now work as when Customers is loaded, EF also loads the data from the related MembershipType table too and you can access properties from that entity.
|
|
|
|
|
Awesome, thank you! Makes a lot of sense now!
|
|
|
|
|
I am trying to understand how MVC Model Binding works so that I can switch from using a Textbox to using a Dropdownlistbox.
In my Edit View I have a form that allows users to Edit a product after modifying its Name, Price, and CategoryId.
The Merchandise's CategoryID is the Primary Key in the Categories table and a Foreign Key in the Merchandise table in the database.
Instead of making users enter a number for the CategoryID I want them to select a CategoryName from a Dropdownlistbox. Once a CategoryName in the Dropdownlistbox is selected the CategoryID is selected automatically.
I have tried to use the following code in my ProductController but it is not working.
[HttpGet]
public ActionResult Edit(string id)
{
MerchandiseServiceClient msc = new MerchandiseServiceClient();
MerchandiseViewModel mvm = new MerchandiseViewModel();
mvm.Merchandise = msc.FindMerchandiseByID(id);
ViewBag.CategoryList = new SelectList(msc.FindAllCategories(), "ID", "Name");
return View("Edit", mvm);
}
[HttpPost]
public ActionResult Edit(MerchandiseViewModel mvm)
{
MerchandiseServiceClient msc = new MerchandiseServiceClient();
msc.EditMerchandise(mvm.Merchandise);
return RedirectToAction("Index");
}
// --------------Edit View-------------------------------------
<tr>
<td>@Html.LabelFor(model=>model.Category.ID, "Category")</td>
<td> @Html.DropDownList("CategoryList", "Select Category")</td>
</tr>
How can I change my code above to use the dropdownlistbox?
modified 19-Jul-16 13:21pm.
|
|
|
|
|
|
Hi, thank you for replying. You suggested that I change my code from
@Html.DropDownList("CategoryList", "Select Category")
to
@Html.DropDownListFor(model => model.Category.ID, ViewBag.CategoryList, "Select A Category")
I got the error has no applicable method named 'DropDownListFor' but appears to have an extension method by that name. So I changed the code to
@Html.DropDownListFor(model => model.Category.ID, (SelectList)ViewBag.listProducts, "Select A Category")
But now I get the error There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'Category.ID'.
|
|
|
|
|
In order to get your code working as it is you need a CategoryList property on your model
public class MerchandiseViewModel
{
public int CategoryList { get; set; }
}
To use DropDownListFor;
public class MerchandiseViewModel
{
public Category Category { get; set; }
}
public ActionResult Test()
{
List<Category> cats = new List<Category>();
cats.Add(new Category { ID = 1, Name = "Category 1" });
cats.Add(new Category { ID = 2, Name = "Category 2" });
cats.Add(new Category { ID = 3, Name = "Category 3" });
ViewBag.CategoryList = cats;
return View();
}
[HttpPost]
public ActionResult Test(MerchandiseViewModel mvm)
{
List<Category> cats = new List<Category>();
cats.Add(new Category { ID = 1, Name = "Category 1" });
cats.Add(new Category { ID = 2, Name = "Category 2" });
cats.Add(new Category { ID = 3, Name = "Category 3" });
ViewBag.CategoryList = cats;
return View(mvm);
}
view;
@using (Html.BeginForm())
{
<p>
@Html.DropDownListFor(m => m.Category.ID, new SelectList(ViewBag.CategoryList, "ID", "Name"), "Select a category")
</p>
<p>
<input type="submit" value="Submit"/>
</p>
}
Note I put the categories in the ViewBag and do the convert to SelectList in the view, that just means that if you want to use the categoires elsewhere on the view you still can.
|
|
|
|
|
Thank you so much for replying. I see what you are trying to do, unfortunately its not working for me since my situation is a little different than what you've posted above.
Below is a snippet of the MerchandiseServiceClient class definition inside of the Models folder
public class MerchandiseServiceClient
{
private string BaseURL = "http://localhost:14000/MerchandiseService.svc/";
public Merchandise FindMerchandiseByID(string id)
{
try
{
var webClient = new WebClient();
string url = string.Format(BaseURL + "FindMerchandiseByID/{0}", id);
var javascriptSerializer = new JavaScriptSerializer();
return javascriptserializer.Deserialize<Merchandise>(jsn);
}
catch
{
return null;
}
}
public List<Category> FindAllCategories()
{
try
{
var webClient = new WebClient();
var jsn = webClient.DownloadString(BaseURL + "FindAllCategories");
var javascriptSerializer = new JavaScriptSerializer();
return javascriptSerializer.Deserialize<List<Category>>(jsn);
}
catch
{
return null;
}
}
.
.
.
.
}
The MerchandiseServiceClient class is one of many classes in the Models folder. The FindAllCategories() method of the MerchandiseServiceClient class gets JSON data from a RESTful WCF service and it returns a List of Category objects.
I tried to use this List of Category objects to populate the dropdownlist but it is not working. Please see my code below.
MerchandiseController
[HttpGet]
public ActionResult Edit(string id)
{
MerchandiseServiceClient msc = new MerchandiseServiceClient();
ViewBag.categoryList = msc.FindAllCategories();
MerchandiseViewModel mvm = new MerchandiseViewModel();
mvm.Merchandise = msc.FindMerchandiseByID(id);
return View("Edit", mvm);
}
Edit View
<tr>
<td>@Html.LabelFor(model=>model.Category.ID, "Category")</td>
<td> @Html.DropDownListFor(model => model.Category.ID, (SelectList)ViewBag.categoryList, "Select A Category")</td>
</tr>
modified 21-Jul-16 11:35am.
|
|
|
|
|
@Html.DropDownListFor(model => model.Category.ID, (SelectList)ViewBag.categoryList, "Select A Category")</
In the above ViewBag.categoryList can't be cast to SelectList as it isn't of that type and can't be converted to that type. You need something similar to my posted code
@Html.DropDownListFor(m => m.Category.ID, new SelectList(ViewBag.categoryList, "ID", "Name"), "Select a category")
Replace "ID" and "Name" with the properties on the Category class that you want to be used for the value and the text.
|
|
|
|
|
Well I'm late to the party here, but since I'm trying to fix my VS2015 SP3 problem here, and waiting for the VS2015 with SP3 to load, I'll throw in my 2 cents.
I found it best to create a
Dictionary<string, string> Categories { get; set; }
So if you have a model
public class model_items
{
int ID { get; set; }
string SKU { get; set; }
int CategoryID { get; set; }
Dictionary<string, string> select_Categories { get; set; }
}
This like FSSiteCore said, you use his example in the View
@Html.DropDownFor(m => m.CategoryID, new SelectList(@Model.selectCategory), "-- Select Category --")
To populate the model in the controller
model_items model = new model_items();
model = load_items(1);
model.selectCategories = load_Categories();
Then you write a function that loads a Dictionary of string string. You can write a function using EF to load the Category Key and value, and then package those as a Dictionary.
I have found this to be the best way to do it for me so far, being on my 3rd MVC project now.
Reason being is well the SelectList in Razor likes Dictionaries and takes them well with no problems.
|
|
|
|
|
Message Closed
modified 19-Jul-16 8:13am.
|
|
|
|
|
Try changing your DropDownList call to:
@Html.DropDownListFor(model => model.Category.ID, ViewBag.CategoryList, "Select Category")
The version you're using at the moment renders a list with the name "CategoryList" , which doesn't seem match any of the properties on your view-model.
Also, it's not clear how the Category property on your view model is implemented. I'm assuming it delegates to a property on the Merchandise ? If not, you'll need to connect the two objects in the POST method.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
how to insert a img in database
|
|
|
|
|
By writing lots of code. Where are you stuck?
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
When your image uploads, it's usually uploaded as a base64 string that represents bytes.
You can just save the Base64 string in the database column, or convert the string to a byte.array, and save the image as bytes in the database. With bytes, you can write the byte array to the disk drive as an image. So get use to converting the data. Another cool thing about storing as bytes is that you can pump those bytes into a function to resize the images, tweak it and send it back out as bytes.
Remember in your database design, to include the filename, the url of where you would want to store the image for the browser to fetch, and the type, jpeg, png.
I have post from 3 months ago that talks about this.
|
|
|
|
|