MVC3 MvcScaffolding EF SqlCE SqlExpress - NO WORKY! - asp.net-mvc-3

Not quite sure what's going on here but it seems as though Microsoft always comes out with the coolest frameworks (in theory) and then leaves no support. Anyone out there that can help me I will be VERY greatful. This one has had me stumped for two days now and I still cannot figure it out. Here's the setup:
Visual Studio 2010 Professional (or Express for that matter, I've tried both)
SQL Server Compact Edition 4 (or Express for that matter, I've tried both)
Create New Project, add new model (I called mine BlogModels.cs)
Add a new class called Post and give it properties as shown below...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
namespace MVC3BlogEngine.Models
{
public class Post
{
public int ID { get; set; }
[Required]
public string Title { get; set; }
[Required]
[DataType(DataType.MultilineText)]
public string Text { get; set; }
public DateTime PublishDate { get; set; }
}
}
Run project, browse to model (/Posts) and attempt to add a new post. If I enter more than say 128 characters, an exception is thrown:
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
So, what gives? I go look at the database that it created (which I think is the coolest thing since sliced bagels) and see that the column that it created was a nvarchar(128). So, my first thought was that even though I told it to use MultilineText as the datatype, it didn't generate a column properly (not good MS). So, I changed the datatype to text and tried again. No luck.
Anyone?

Well, it would seem that at last resort of removing the attribute of the datatype resolved the issue. The only reason I had it in there to start with was because of Scott Hanselman's video blog entry on creating an MVC3 Blog and he used the attribute in his example (November 2010) and MVC3 was still in development. So, FYI, DataType.MultilineText will not allow you to enter over 128 characters.

Related

Model Validation using Data Annotations not working on Mac VS.NET

I created a new Web API Application on MacOS with VS.NET. I added a model with the following code:
public class PersonAddRequest {
[Required]
public string FirstName { get; set; }
public string LastName { get; set; }
}
Then I created a Web API Controller like so:
[Route("api/[controller]")]
[ApiController]
public class PeopleController : ControllerBase {
[HttpPost]
public IActionResult Post(PersonAddRequest model) {
return base.Ok(model);
}
}
When using PostMan I post JSON as so (note firstName is missing):
{
"lastName":"McDonalds"
}
When I run this code on a Web API Application created on a Windows OS validation works as expected. However, when I create the project in MacOS validation doesn't work.
When I create the project in Windows OS then open and run it on MacOS, validation works!
So my question, is this a feature that is missing as part of the scaffolding on VS.NET for Mac?
You require the FromBody attribute for the body argument:
[Route("api/[controller]")]
[ApiController]
public class PeopleController : Controller
{
[HttpPost]
public IActionResult Post([FromBody]PersonAddRequest model)
{
return Ok(model);
}
}
This is required for model validation.
Here's an example git repo using this form of model validation that is confirmed working on MacOS Mojave 10.14.4
I want to thank #nelsontruran for helping me explore VS.NET on Mac. I'm actually running a later version of the Mac (10.14.5 Mojave). However, the version had nothing to do with the original problem.
Thru the patients and persistent help of #nelsontruran I recreated the project but instead of using the suggested templates I dug around a little for the correct application (don't trust the suggested applications in VS.NET). Here's the step-by-step instructions on how to create an API that automatically performs model validation:
Open VS.NET for Mac
Create a new project
When prompted to "Choose a template for your new project", select "App" under the ".Net Core" section on the left side pane.
On the right side pane, select API
From there you can review #nelsontruran's example on git to see the magic of Model Validation.
I believe I might have been choosing "Web Application" thinking that it would be the same in Mac as it is in Windows.
A simple oversight on my part. I hope this helps someone who was confused and struggling like me.
Thank you again to #nelsontruran!!!

MVC Entity Framework Conundrum: "missing using directive or an assembley reference?"

This project used to work. I fuddled with the connection string in Web.Config trying to get it to work with .accdb file. Forget that.
But now I can't get back home to a working project.
HomeController.cs code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using oesac.Models;
namespace oesac.Controllers
{
[HandleError]
public class HomeController : Controller
{
oesacEntities_connection _db;
public HomeController()
{
_db = new oesacEntities_connection();
}
=============================
E:\oesac_MVC\Controllers\HomeController.cs(52,42): error CS1061: 'oesac.Models.oesacEntities_connection' does not contain a definition for 'Courses' and no extension method 'Courses' accepting a first argument of type 'oesac.Models.oesacEntities_connection' could be found (are you missing a using directive or an assembly reference?)
(_db.Courses is underlined, just Courses has the underline)
//SEARCH BY COURSE TITLE
var courses = (from m in _db.Courses select m);
=======================
The Entity Container Name is:
"oesacEntities_connection"
It has 2 tables, Courses and Sponsors, and refreshes tables when I right click in .edmx window and select "Update Model From Database".
So database is there and VS 2010 sees it.
But for some reason there is a disconnect in referencing all this.
If I breeze through the popup after the errors that says "would you like to continue" I get a webpage with this error:
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Parser Error Message: Could not load type 'oesac.MvcApplication'.
Source Error:
Line 1: <%# Application Codebehind="Global.asax.cs" Inherits="oesac.MvcApplication" Language="C#" %>
And this is what is in Global.asax.cs:
namespace oesac
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
I'm thinking if it was snake it would have bit me. But a little too new at this MVC dreamworld.
Ok, if someone else understands this better I can give them the green checkmark for correct answer. However, a friend at work said sometimes with EF you just have to wipe the slate clean and add a new Entity Framework class. And not get too clever. I had changed the names of the tables in VS 2010 .edmx file like it says you can and somehow the Gremlins took over.
Friend at work and 2 other people came to realize this after pulling out all their hair on one of there first major MVC projects.
And, yes, I did try all the mumbo jumbo of "Update Model From Database" right click in the .edmx in VS.

RelatedTo attribute not recognised, even after installation of Entity Framework 4.1

I am busy learning ASP.Net MVC and so I recently installed MVC 4 on Visual Studio 2010 Professional. I was trying this example:
http://www.luisrocha.net/2010/11/creating-composite-keys-using-code.html
But the RelatedTo attribute is not found by Visual Studio. I read on this question that this could be caused by Entity Framework 4.1 not being installed. I checked the version number of System.Data.Entity.dll and it gave version 4.0.0.0, runtime version v4.0.30319. So this seems to me to be version 4.0 of EF that is installed on my computer. I may be wrong on this though... so someone please correct me if I am.
I downloaded EF 4.1 from here and installed it, but the version numbers for System.Data.Entity.dll seem not to have changed and the RelatedTo attribute is still not working. Anyone know what is going wrong with the installation and how I could fix it?
UPDATE:
I installed Entity Framework using nuget as instructed on this site, and the console said it installed EntityFramework 5.0.0-rc, but the version of System.Data.Entity.dll seems to be unchanged.
No, I did not include using System.Data.Entity because intellisense did not tell me to include that. I did include System.ComponentModel.DataAnnotations and using System.ComponentModel.DataAnnotations.Schema because these were required by the Key and Column attributes. Intellisense asks me to "Generate class for 'RelatedTo'" and adding using System.Data.Entity does not change that.
Here is my code for those asking for it, although it is exactly the code from the tutorial that I linked to in the question:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
namespace Example
{
public class PlaylistTrack
{
[Key, Column(Order = 1)]
public int PlaylistId { get; set; }
[Key, Column(Order = 2)]
public int TrackId { get; set; }
[RelatedTo(ForeignKey = "PlaylistId")]
public Playlist Playlist { get; set; }
[RelatedTo(ForeignKey = "TrackId")]
public Track Track { get; set; }
}
}
Try installing new version of EF using Nuget. It will resolve all dependencies of EF and it will make sure you have the latest version. I don't know what is RelatedTo attribute supposed to be doing here. And see here in the data annotations section, the related to attribute is not in the EF4.1 release. But you can do the same thing like this.
[ForeignKey("PlaylistId")]
public Playlist Playlist { get; set; }
[ForeignKey("TrackId")]
public Track Track { get; set; }
EDIT
RelatedTo attribute is replaced by ForeignKey and InverseProperty attributes in EF 4.1 RC

Additional Metadata Attribute Ignored – in one instance of VS2010 but not the other

I have 2 copies of VS2010 Ultimate, one running on a box with XP SP3 and the other on Win7; both 32 bit. Both are otherwise identical, as far as I can tell.
I created a little test MVC3 project including a simple model with a couple of AdditionalMetadata attributes on it. Run the ap on the XP box with breakpoints at appropriate places in the view and sure enough, there are my AdditionalMetadata values where I expect under ViewData > ModelMetadata > AdditionalValues.
I copy the project to my Win7 machine and load it into that copy of VS 2010. Builds fine. Run it in debug mode and… no sign of the AdditionalMetadata values. It's as though they were not added to the model.
I just upgraded both copies to SP1 with no effect. Is there a funky setting somewhere I’m missing? I dread having to rebuild the machine.
Here’s my simple model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
namespace Tp4.Models
{
[System.Web.Mvc.AdditionalMetadata("AdditionalKey1", "Foo")]
public class Person
{
public int PersonId { get; set; }
[Required]
public string FullName { get; set; }
[UIHint("PlaceholderString")]
[System.Web.Mvc.AdditionalMetadata("AdditionalKey2", "Bar")]
public string Title { get; set; }
}
}

Problem with MVC3 AllowHtml attribute

I have a web app based on MVC3 (no beta or release candidate, RTM/RTW version) that has an action that accepts XML files for processing.
Of course, this seems evil to MVC because of possible attacks, so it doesn't allow it. Well, I try to put either AllowHtml on the model object as such:
public class XMLModel
{
[AllowHtml]
public string msg { get; set; }
}
Or I set ValidateInput to false on my action method such as this:
[ValidateInput(false)]
public ActionResult AddCDR(XMLModel model)
{
}
The reason for having a "strongly" typed model in the first place was that I originally tried to have a string value named "msg" as the action method parameter, but that always came back empty.
Now, when someone posts to this form, either on the same machine or from a networked computer, the msg field is always blank.
I have verified with WireShark that the data is actually in the request.
Now, one interesting thing. This should not be necessary with MVC 3. Yet it makes a slight difference.
If I add this to my web config:
<httpRuntime requestValidationMode="2.0" />
It works for requests originating from the local computer, but it does NOT work from another system.
I think the AllowHtml version seems elegant - if only it worked.
I have also found out about a bug in RC2 - again, this should not affect me, but I tried to add the following in Application_Start() anyway:
ModelMetadataProviders.Current = new DataAnnotationsModelMetadataProvider();
As expected, it makes no real difference.
Everything works as expected on my development computer (Win7x64, VS2010), but on the target system (Win2008R2x64, IIS7.5) the above problems are giving me a hard time.
Very important point to note: If I post to the action with no angle brackets, I get the form data as expected. As soon as the angle brackets show up, though, either my action isn't called at all, or it doesn't find the form data, neither in action method parameters or in for instance Request.Params["msg"] where it should be.
Any ideas to allow the XML through?
UPDATE
I've tried to work my way around this while waiting for anyone to come up with an answer here. I have not been able to solve it yet, but I have checked a few additional details;
First, I verified that the ASP.NET MVC 3 version installed on both my development computer and on the web server is the same; v3.0.20105.0 (according to Add/Remove programs).
The web application was actually created with the MVC3 beta, then converted to RC1 and RC2 as they came out, and finally to the RTM version. This meant I had to modify some code because of the ViewBag change. However, since I didn't know exactly what else had changed, I created a brand new MVC3 application with the RTM version, created a controller by the same name, with the same action method, taking a model object similar to the currently used one, renamed the old web app directory and put this new one in place. Exactly the same thing happens - i.e. the parameter with the AllowHtml attribute comes through with all content when the request is made from the local machine, but if it comes from a remote machine, then the HTML (XML actually) is stripped out.
There is no exception. I've installed Elmah and verified this - no exception occurs, my action method is apparently just called with anything looking like XML stripped out from the parameters to the method.
Needless to say, this is driving me crazy. I had some advice yesterday to look at the source code of MVC3, but unfortunately that doesn't really help me, as I can't see anything obviously wrong there, and I can't debug code on the server in question.
Any insights still highly desired, thanks! :)
I cannot reproduce this.
Model:
using System.Web.Mvc;
namespace MvcApplication3.Models {
public class XmlModel {
[AllowHtml]
public string msg { get; set; }
}
}
Controller:
using System.Web.Mvc;
using MvcApplication3.Models;
namespace MvcApplication3.Controllers {
public class HomeController : Controller {
public ActionResult Index() {
return View();
}
[HttpPost]
public ActionResult Index(XmlModel model) {
return View();
}
}
}
View:
#model MvcApplication3.Models.XmlModel
#using (Html.BeginForm()) {
<div class="editor-label">
#Html.LabelFor(model => model.msg)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.msg)
#Html.ValidationMessageFor(model => model.msg)
</div>
}
These were added to a default empty MVC 3 project. Everything posts fine. When I remove [AllowHtml], it generates the appropriate error message.
I am answering this myself because this was an obscure situation, and MVC3 was not the problem.
The system that POSTed data to my application was doing so with malformed data. In addition to that, I was told that they were also POSTing to an older MVC2 application that worked just fine - this was wrong, it turned out they were GETting in that case, and GET they happen to do correctly.
If anything, fairly thorough testing has confirmed that the AllowHtml attribute actually works very well. Never trust your input, tho. :)

Resources