I want to update only one filed of the DB. I am creating a function in my Controller (AOProf Controller) that looks like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Accommodation_App.Models;
using System.Data.Entity;
namespace Accommodation_App.Controllers
{
public class AOProfController : Controller
{
private StudAccommodationEntities1 SContextUser;
private DbSet<User> UserDb;
private StudAccommodationEntities1Entities SContextProp;
private DbSet<Property> Properties;
public AOProfController()
{
SContextUser = new StudAccommodationEntities1();
UserDb = SContextUser.UserDb;
SContextProp = new StudAccommodationEntities1Entities();
Properties = SContextProp.Properties;
}
[HttpPost]
public ActionResult Reject(int id)
{
Property obj = Properties.Find(id);
if (obj != null)
{
obj.isApproved = false;
UpdateModel(obj);
SContextProp.SaveChanges();
}
else{
return Content("Not such an object");
}
return Content("the rejected is confirmed");
}
And in my view I call this function from the onclick event. I sent the param before to the view. It is send correctly.
<button value="Reject" onclick="location.href='#Url.Action("Reject", "AOProf", new { id = item.ProId })'" >Reject</button>
The url that appears in the URL bar after clicking the button is
http://localhost:54464/AOProf/Reject/1
It looks fine, but the record won't update in the database. It will stay the same. Can you please, help me to understand how to to do it the right way?
Related
We have this very weird TournamentBatch Razor Page (index.cshtml) where we have something like this:
<td>
<a asp-page="/TournamentBatchItems/Index" asp-route-id="#item.TournamentBatchID">Items</a> |
<a asp-page="./Edit" asp-route-id="#item.TournamentBatchID">Edit</a> |
<a asp-page="./Details" asp-route-id="#item.TournamentBatchID">Details</a> |
<a asp-page="./Delete" asp-route-id="#item.TournamentBatchID">Delete</a>
</td>
and when we are running this and end up that there is no id returned on the page just for /Delete link and other links are ok (/TournamentBatchItems/Index, /Edit, /Details).
This is the html source looks like:
<td>
Items |
Edit |
Details |
Delete |
</td>
Now other pages for Delete are OK only this page. ?!?!
Any ideas?
Index Pagemodel:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using AthlosifyWebArchery.Data;
using AthlosifyWebArchery.Models;
using AthlosifyWebArchery.Utilities;
using CsvHelper;
using System.IO;
namespace AthlosifyWebArchery.Pages.TournamentBatches
{
public class IndexModel : PageModel
{
private readonly AthlosifyWebArchery.Data.ApplicationDbContext _context;
public IndexModel(AthlosifyWebArchery.Data.ApplicationDbContext context)
{
_context = context;
}
public IList<TournamentBatch> TournamentBatches { get;set; }
public async Task OnGetAsync()
{
TournamentBatches = await _context.TournamentBatch.ToListAsync();
}
}
}
Delete Pagemodel:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using AthlosifyWebArchery.Data;
using AthlosifyWebArchery.Models;
namespace AthlosifyWebArchery.Pages.TournamentBatches
{
public class DeleteModel : PageModel
{
private readonly AthlosifyWebArchery.Data.ApplicationDbContext _context;
public DeleteModel(AthlosifyWebArchery.Data.ApplicationDbContext context)
{
_context = context;
}
[BindProperty]
public TournamentBatch TournamentBatch { get; set; }
public string ConcurrencyErrorMessage { get; set; }
public async Task<IActionResult> OnGetAsync(Guid? id, bool? concurrencyError)
{
if (id == null)
{
return NotFound();
}
TournamentBatch = await _context.TournamentBatch
.AsNoTracking() //Addded
.FirstOrDefaultAsync(m => m.TournamentBatchID == id);
if (TournamentBatch == null)
{
return NotFound();
}
if (concurrencyError.GetValueOrDefault())
{
ConcurrencyErrorMessage = "The record you attempted to delete "
+ "was modified by another user after you selected delete. "
+ "The delete operation was canceled and the current values in the "
+ "database have been displayed. If you still want to delete this "
+ "record, click the Delete button again.";
}
return Page();
}
public async Task<IActionResult> OnPostAsync(Guid? id)
{
/*if (id == null)
{
return NotFound();
}
TournamentBatch = await _context.TournamentBatch.FindAsync(id);
if (TournamentBatch != null)
{
_context.TournamentBatch.Remove(TournamentBatch);
await _context.SaveChangesAsync();
}
return RedirectToPage("./Index");
*/
try
{
if (await _context.TournamentBatch.AnyAsync(
m => m.TournamentBatchID == id))
{
// Department.rowVersion value is from when the entity
// was fetched. If it doesn't match the DB, a
// DbUpdateConcurrencyException exception is thrown.
_context.TournamentBatch.Remove(TournamentBatch);
await _context.SaveChangesAsync();
}
return RedirectToPage("./Index");
}
catch (DbUpdateConcurrencyException)
{
return RedirectToPage("./Delete",
new { concurrencyError = true, id = id });
}
}
}
}
Reference: Anchor Tag Helper
Usually this happens when the anchor tag helper (in this case, asp-page) can't find the page or can't resolve the route through the default route conventions (you can find more details, or how to customize the conventions here
First check and make sure your Delete.cshtml page is in the same spot as Edit.cshtml and Details.cshtml (since they are working, and you used the same relative path for all 3.)
Also check and that you've started your page with the #page directive, and that your page directive is in the correct syntax which matches the appropriate method signature in your PageModel
example: #page "{id:int}"
public IActionResult OnGet(int id)
{
...
}
I am an experienced programmer but relatively new to c# mvc. I am attempting to create my first viewmodel to combine two models into one so a view can access members from both. I have followed instructions on combining distinct models into one view model:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ViApplication.Models;
using System.ComponentModel.DataAnnotations;
namespace ViApplication.ViewModel
{
public class TemplateMTMQuestionViewModel
{
public TemplateVISpdat ThisTemplate { get; set; }
public MtmTemplateViSpdatQuestion ThisMTMQuestion { get; set; }
}
}
I have created a controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ViApplication.ViewModel;
using ViApplication.Models;
using System.Net;
namespace ViApplication.Controllers
{
public class TemplatesMTMQuestions : Controller
{
private VulnerabilityIndexDatabaseEntities db = new VulnerabilityIndexDatabaseEntities();
public ActionResult AddQuestionToTemplate(long? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
TemplateVISpdat templateVISpdat = GetTemplateByID(id);
if (templateVISpdat == null)
{
return HttpNotFound();
}
TemplateMTMQuestionViewModel TMTMQVM = new TemplateMTMQuestionViewModel();
TMTMQVM.ThisTemplate = GetTemplateByID(id);
TMTMQVM.ThisMTMQuestion = GetBlankMtmTemplateViSpdatQuestion();
return View(TMTMQVM);
}
public TemplateVISpdat GetTemplateByID(long? id)
{
TemplateVISpdat templateVISpdat = db.TemplateVISpdats.Find(id);
return templateVISpdat;
}
public MtmTemplateViSpdatQuestion GetBlankMtmTemplateViSpdatQuestion()
{
MtmTemplateViSpdatQuestion TMTMQVM = new MtmTemplateViSpdatQuestion();
return TMTMQVM;
}
}
}
This compiles fine. But when I try to create a view from AddQuestionToTemplate and select Empty and my ViewModel I get:
Unable to retrieve metadata for
ViApplication.ViewMdoel.TemplateMTMQuestionViewModel. One or more
validation errors were detected during model generation.
TemplateMTMQuestionViewModel::EntityType TemplateMTMQuestionViewModel
has no key defined
The only difference between this project and other projects is that I am using database first.
Any help would be greatly appreciated.
I am trying to access a table using its controller from another controller method.
But when the method tries to call the table controller method I get an exception:
Exception=System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.WindowsAzure.Mobile.Service.TableController.....
I manage to access the table controller method from the web API and execute it successfully.
I tried the same thing with TodoItem given as an example by the initial mobile service.
After several publishes to the server trying to fix the issue the web API stopped working and I get this exception : An exception of type 'Microsoft.WindowsAzure.MobileServices.MobileServiceInvalidOperationException' occurred in mscorlib.dll but was not handled in user code
Additional information: The request could not be completed. (Internal Server Error) I managed to solve it when I reopened a mobile service and database with the exact same code that didn't work.
Any tips ?
Here is my table controller created by the controller wizard:
using System.Linq;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.OData;
using Microsoft.WindowsAzure.Mobile.Service;
using FringProjectMobileService.DataObjects;
using FringProjectMobileService.Models;
namespace FringProjectMobileService.Controllers
{
public class StorageItemController : TableController<StorageItem>
{
protected override void Initialize(HttpControllerContext controllerContext)
{
base.Initialize(controllerContext);
FringProjectMobileServiceContext context = new FringProjectMobileServiceContext();
DomainManager = new EntityDomainManager<StorageItem>(context, Request, Services);
}
// GET tables/StorageItem
public IQueryable<StorageItem> GetAllStorageItem()
{
return Query();
}
// GET tables/StorageItem/xxxxxxxxxx
public SingleResult<StorageItem> GetStorageItem(string id)
{
return Lookup(id);
}
// PATCH tables/StorageItem/xxxxxxxx
public Task<StorageItem> PatchStorageItem(string id, Delta<StorageItem> patch)
{
return UpdateAsync(id, patch);
}
// POST tables/StorageItem
public async Task<IHttpActionResult> PostStorageItem(StorageItem item)
{
StorageItem current = await InsertAsync(item);
return CreatedAtRoute("Tables", new { id = current.Id }, current);
}
// DELETE tables/StorageItem/xxxxxxxxxx
public Task DeleteStorageItem(string id)
{
return DeleteAsync(id);
}
}
}
Below the other controller code trying to access the method:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Microsoft.WindowsAzure.Mobile.Service;
namespace FringProjectMobileService.Controllers
{
public class ArduinoController : ApiController
{
public ApiServices Services { get; set; }
// GET api/Arduino
public string Get()
{
Services.Log.Info("Hello from custom controller!");
return "Hello";
}
public async void PostProcessTag(String id)
{
Microsoft.WindowsAzure.MobileServices.MobileServiceClient client = new Microsoft.WindowsAzure.MobileServices.MobileServiceClient("http://some-service.azure-mobile.net", "XXXXXXXXXXXXXXX");
Microsoft.WindowsAzure.MobileServices.IMobileServiceTable<DataObjects.StorageItem> storage_item_table = client.GetTable<DataObjects.StorageItem>();
await storage_item_table.ToEnumerableAsync();
}
}
}
I also tried a different implementation for the method :
public void PostProcessTag(String id)
{
StorageItemController table_controller = new StorageItemController();
IQueryable<DataObjects.StorageItem> item = table_controller.GetAllStorageItem();
}
The service context:
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using Microsoft.WindowsAzure.Mobile.Service;
using Microsoft.WindowsAzure.Mobile.Service.Tables;
namespace FringProjectMobileService.Models
{
public class FringProjectMobileServiceContext : DbContext
{
// You can add custom code to this file. Changes will not be overwritten.
//
// If you want Entity Framework to alter your database
// automatically whenever you change your model schema, please use data migrations.
// For more information refer to the documentation:
// http://msdn.microsoft.com/en-us/data/jj591621.aspx
//
// To enable Entity Framework migrations in the cloud, please ensure that the
// service name, set by the 'MS_MobileServiceName' AppSettings in the local
// Web.config, is the same as the service name when hosted in Azure.
private const string connectionStringName = "Name=MS_TableConnectionString";
public FringProjectMobileServiceContext() : base(connectionStringName)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
string schema = ServiceSettingsDictionary.GetSchemaName();
if (!string.IsNullOrEmpty(schema))
{
modelBuilder.HasDefaultSchema(schema);
}
modelBuilder.Conventions.Add(
new AttributeToColumnAnnotationConvention<TableColumnAttribute, string>(
"ServiceTableColumn", (property, attributes) => attributes.Single().ColumnType.ToString()));
}
public System.Data.Entity.DbSet<FringProjectMobileService.DataObjects.StorageItem> StorageItems { get; set; }
}
}
I am trying to create a web api with forms based authentication. I want to login from a client and retrieve data from there. When I log in, user gets authenticated and can retrieve data by giving http request direct into adressbar like localhost:1393/api/Game. But when i try to get it from client I am getting a 401 (Unauthorized error). I have enabled CORS in server side. This is the controller to handle data
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using System.Web.Security;
using Cheeky_backend.Models;
using System.Web.Http.WebHost;
namespace Cheeky_backend.Controllers
{
public class Demo
{
public List<Teams> team { get; set; }
public List<Hole> hole { get; set; }
}
[Authorize]
public class GameController : ApiController
{
private Cheeky_backendContext db = new Cheeky_backendContext();
// GET api/Game
public IEnumerable<Hole> GetHoles()
{
return db.Holes.AsEnumerable();
}
}
}
This is the authenticating controler
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Security;
using System.Web.Http;
using Cheeky_backend.Models;
namespace Cheeky_backend.Controllers
{
public class UserController : ApiController
{
private Cheeky_backendContext db = new Cheeky_backendContext();
// GET api/Default1
// GET api/Default1/5
// PUT api/Default1/5
// POST api/Default1
public HttpResponseMessage CreateUser(User user)
{
if (ModelState.IsValid)
{
db.Users.Add(user);
db.SaveChanges();
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, user);
// response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = user.ID }));
return response;
}
else
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}
// DELETE api/Default1/5
public HttpResponseMessage Login(User user)
{
var userfound = from user2 in db.Users
where user.username == user2.username && user.password == user2.password
select user2;
if( userfound.Any())
{
FormsAuthentication.SetAuthCookie(user.username, true);
return Request.CreateResponse(HttpStatusCode.OK,user);
}
return Request.CreateResponse(HttpStatusCode.Unauthorized);
}
}
}
Source
In your Authentication Handler
Don't set the Principal on the Thread.CurrentPrinicipal any more.
Use the Principal on the HttpRequestContext.
Take a look at here
I'm trying to link my MVC project with a database using Entity Framework, but I can't figure out how to store new records to a database. So far I have the following code:
//model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using MvcApplication1.Controllers;
namespace MvcApplication1.Models
{
public class CarUser
{
public int ID { get; set; }
[DisplayName ("First Name")]
public string Fname { get; set; }
[DisplayName ("Surname")]
public string Sname { get; set; }
}
}
//controller
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
namespace MvcApplication1.Controllers
{
public class CarUserController : Controller
{
private FormDataContext db = new FormDataContext();
//
// GET: /CarUser/
public ActionResult Index()
{
return View(db.CarUsers.ToList());
}
//
// GET: /CarUser/Details/5
public ActionResult Details(int id = 0)
{
CarUser caruser = db.CarUsers.Find(id);
if (caruser == null)
{
return HttpNotFound();
}
return View(caruser);
}
//
// GET: /CarUser/Create
public ActionResult Create()
{
return View();
}
//
// POST: /CarUser/Create
[HttpPost]
public ActionResult Create(CarUser caruser)
{
if (ModelState.IsValid)
{
db.CarUsers.Add(caruser);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(caruser);
}
//
// GET: /CarUser/Edit/5
public ActionResult Edit(int id = 0)
{
CarUser caruser = db.CarUsers.Find(id);
if (caruser == null)
{
return HttpNotFound();
}
return View(caruser);
}
//
// POST: /CarUser/Edit/5
[HttpPost]
public ActionResult Edit(CarUser caruser)
{
if (ModelState.IsValid)
{
db.Entry(caruser).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(caruser);
}
//
// GET: /CarUser/Delete/5
public ActionResult Delete(int id = 0)
{
CarUser caruser = db.CarUsers.Find(id);
if (caruser == null)
{
return HttpNotFound();
}
return View(caruser);
}
//
// POST: /CarUser/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
CarUser caruser = db.CarUsers.Find(id);
db.CarUsers.Remove(caruser);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
//formdatacontext
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace MvcApplication1.Models
{
public class FormDataContext : DbContext
{
public DbSet<CarUser> CarUsers
{ get; set; }
}
}
//formdatainitialiser
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.IO;
namespace MvcApplication1.Models
{
public class FormDataInitializer : DropCreateDatabaseAlways<FormDataContext>
{
protected override void Seed(FormDataContext context)
{
base.Seed(context);
var CarUsers = new List<CarUser>
{
new CarUser {
ID = 1,
Fname = "Craig",
Sname = "Higginson",
}
};
CarUsers.ForEach(s => context.CarUsers.Add(s));
context.SaveChanges();
}
}
}
I've also got the following in my web.config file
<connectionStrings>
<add name="FormDataContext"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=FormData.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
This all builds fine, and when I navigate to /caruser i am able to create new records. These records aren't being stored in my database.
Do I need to create the table in the database first or should Entity Framework create this for me, if i have referenced the database correctly?
If you're using a Code First approach then Entity Framework will build the table for you. It looks like you are not using Code First, so you will have create the table in the database. I assume you're using an Entity Data Model (.edmx)? If so, you will create your table in the database, then update your data model (.edmx). If you have not yet created your .edmx file, you need to do that - the .edmx file will contain all your CRUD operations.
What I'm confused about is I'd imagine your code would throw an error if the table did not exist (i.e. if the table represented by your data model didn't map to an actual table in the database, because it doesn't exist). So, the question is, does your table already exist? If it does, then step through the code line by line to find out why your records aren't being saved. If it doesn't exist, then add the table via SQL Server Management Studio (or similar), then open your .edmx file, right click on the layout that comes up, click "Update Model from database".
You can find all needed info here:
creating-an-entity-framework-data-model-for-an-asp-net-mvc-application
First you have add an edmx project to your existing solution which is a mockup of db and contains all the tables and SPs.
From your action method make a call to one more class, say DataAccessLayer.cs.
In DataAccessLayer.cs, add a reference to your edmx project.
Use this code:
using (var entity = new FormDataContext())
{
//entity. will give you all the tables in the database.
}