Pass data between controllers using method void - asp.net

I need to pass values from one controllaro to a method from another controller.
or is there another way to pass data to an object so that object takes care of doing all the operations receiving values from other controllers
PRIMARY CONTROLLER
DateTime TimeLog = DateTime.Now;
LogController.Insert(TimeLog);
LOG CONTROLLER
using PROGRAM.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace PROGRAM.Controllers
{
public class LogController : Controller
{
Entities db = new Entities();
internal static void Insert(DateTime TimeLog)
{
LogModel log = new LogModel();
log.User = Session["User"].ToString();
log.TimeLog = TimeLog;
db.Log_Arqueo.Add(log);
db.SaveChanges();
}
}
}
ERROR
Severity Code Description Project File Line Status deleted
Error CS0120 An object reference is required for the non-static 'Controller.Session' field, method, or property PROGRAM\Controllers\LogController.cs 17 Active

Related

Dapper.Contrib methods unavailable for IDbConnection object

I am trying to use Dapper.Contrib to extend the functionality of the IDbConnection interface with, amongst others, the .insert() method.
To do so, I have followed the somewhat brief and scattered documentation here and here. In short, I have used NuGet to add Dapper and Dapper.Contrib to my project, I have added using Dapper; and using Dapper.Contrib; at the top of my Repository class, and I am using System.Data.SqlClient.SqlConnection() to create an IDbConnection.
Still, my connection object does not have the extended methods available. For example, when trying to use the .insert() method, I get the message:
'IDbConnection' C# does not contain a definition for 'Insert' and no
extension method 'Insert' accepting a first argument of type could be
found (are you missing a using directive or an assembly reference?)
This is in an ASP.NET Core 2.0 project using Razor Pages.
For completeness sake, you can find the Repository class below.
Maybe interesting to note, is that the using lines for Dapper and Dapper.Contrib are grayed out...
Also, of course I have a (very minimalistic) Model Class for the TEST Entity, containing one parameter, TEST_COLUMN, annotated with [Key].
using Dapper.Contrib;
using Dapper;
using Microsoft.Extensions.Configuration;
using TestProject.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Threading.Tasks;
namespace TestProject.Repository
{
public class TEST_Repository
{
IConfiguration configuration;
public TEST_Repository(IConfiguration configuration)
{
this.configuration = configuration;
}
public void Insert()
{
using (var con = this.GetConnection())
{
con.Insert(new TEST { TEST_COLUMN = "test" });
}
}
public IDbConnection GetConnection()
{
return new SqlConnection(configuration.GetSection("ConnectionStrings").GetSection("DefaultConnection").Value);
}
}
}
The Insert method you are looking for lives inside of the Dapper.Contrib.Extensions namespace, as can be seen in the source, included for completeness:
namespace Dapper.Contrib.Extensions
{
...
public static long Insert<T>(this IDbConnection connection, ...)
...
}
Hence, in order to use the Extension methods, you should add the following line to your code:
using Dapper.Contrib.Extensions;

Invoke C# .NET Core Lambda from AWS CodePipeline Action

AWS CodePipeline allows you to invoke a custom Lambda from an action as described here, https://docs.aws.amazon.com/codepipeline/latest/userguide/actions-invoke-lambda-function.htmltion
I am having trouble determining how my C# Lambda function should be defined in order to access the input data from the pipeline.
I tried numerous attempts, was thinking it would be something similar to below. I have also tried to create my own C# classes that the input JSON data would be deserialized to.
public void FunctionHandler( Amazon.CodePipeline.Model.Job
CodePipeline, ILambdaContext context)
I was able to find out a solution. Initially the first step that helped was to change the input parameter for my lambda function to a Stream. I was then able to convert the stream to a string and determine exactly what was being sent to me, e.g
public void FunctionHandler(Stream input, ILambdaContext context)
{
....
}
Then, based on the input data I was able to map it to a C# class that wrapped the AWS SDK Amazon.CodePipeline.Model.Job class. It had to be mapped to the json property "CodePipeline.job". The below code worked, I was able to retrieve all input values.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Amazon.Lambda.Core;
using Amazon.CodePipeline;
using Newtonsoft.Json;
using System.IO;
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
namespace lambdaEmptyFunction
{
public class Function
{
public class CodePipelineInput
{
[JsonProperty("CodePipeline.job")]
public Amazon.CodePipeline.Model.Job job { get; set; }
}
public void FunctionHandler(CodePipelineInput input, ILambdaContext context)
{
context.Logger.LogLine(string.Format("data {0} {1} {2}", input.job.AccountId, input.job.Data.InputArtifacts[0].Location.S3Location.BucketName, input.job.Id));
}
}
}

How can i using GetObjectByKey in mvc 5 using entity framword 6

I am using mvc 5 and ef 6 to build a project. I was simplify the retrieval code by adding some methods to partial class entity context. This is how i do it:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Entity;
using System.Linq.Expressions;
using System.Data.Entity.Core;
namespace MyShop.Models
{
public partial class DataEntities
{
public T GetById<T>(object id) where T : class
{
EntityKey key = CreateKey<T>(id);
return (T)GetObjectByKey(key);
}
private EntityKey CreateKey<T>(object id)
{
var type = typeof(T);
return new EntityKey("MyEntities." + "." + type.Name, "Id", id);
}
}
But it error at GetObjectByKey though i was add entityframwork 6.dll in my project, and i tried add some references but it don't work.
Can you help me fix the error.
Thanks you for read.
Phuong

Populating a Telerik Grid using ViewModel (MVC3)

Ok, this is a really newbie question but I am stumped.
I am trying to use a ViewModel to get my data from an entity object and populate the telerik mvc grid.
At this point I am a bit confused and need your help.
I understand the error message but I am not sure how I need to fix this since I am really new to MVC.
----ERROR----
Error 1 Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Linq.IQueryable'. An explicit conversion exists (are you missing a cast?) ProjectRepository.cs 23 20 MvcMyAPP
I have this viewmodel:
--VIEWMODEL--
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;
namespace mvcMyModel.ViewModels
{
public class ProjectViewModel
{
public IQueryable<mvcMyAPP.Models.ProjectRepository> ProjectList
{
get;
set;
}
}
}
--CONTROLLER--
namespace MvcMyAPP.Controllers
{
public class HomeController : Controller
{
// GET: /Home/
ProjectRepository Repository = new ProjectRepository();
public ActionResult Index()
{
ProjectViewModel objProjectViewModel = new ProjectViewModel();
objProjectViewModel.ProjectList = Repository.GetProjects();
return View(objProjectViewModel);
return View();
}
}
}
----REPOSITORY (MODEL)-----
namespace mvcMyAPP.Models
{
public class ProjectRepository
{
mvcMyAPP.Models.MYEntities MYDB = new MYEntities();
//Fetching data from table
public IQueryable<mvcMyAPP.ViewModels.ProjectViewModel> GetProjects()
{
var vProjects = (from tblProjects in MYDB.Projects
select tblProjects);
return vProjects;
}
}
---GRID---
#{Html.Telerik().Grid(Model.ProjectList)
.Name(
"Grid")
.Pageable()
.Sortable()
.Filterable()
.Groupable()
.Render();
}
You don't need the second return View() in the controller, but that isn't causing any problems.
You might try putting a cast on the return variable from your repository so it is returning the type specified in return:
// return vProjects;
// cast the return variable to return type
return (IQueryable<mvcMyAPP.ViewModels.ProjectViewModel>)vProjects;
I highly recommend Microsoft site for learning MVC, they have a great starting point for learning MVC.
http://www.asp.net/mvc

recursive function for hierarchically data

my model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace amief.Models
{
public class WebsiteModels
{
public static void getPagesForPage(int pageId, dbDataContext db, List<page> myPages)
{
var pages = (from p in db.pages
where p.pageParent == pageId
select p);
foreach (var item in pages)
{
myPages.Add(item);
getPagesForPage(item.pageId, db, myPages);
}
}
}
}
calling the procudure
List<page> myPages = null;
WebsiteModels.getPagesForPage(0, db,myPages);
i'm getting an error
System.NullReferenceException was unhandled by user code
Message=Object reference not set to an instance of an object.
on line "myPages.Add(item);"
I don't understand the error...
You're setting myPages to null before passing it to WebsiteModels.getPagesForPage(). Therefore, the calls to myPages.Add(item); in your loop raise a NullReferenceException because you can't call a method on a null object.
You probably want:
List<page> myPages = new List<page>();
WebsiteModels.getPagesForPage(0, db, myPages);
Well, "myPages" IS null, so calling a method on it results i a NullReferenceException. You should rather write
myPages = new List<page>();

Resources