I have the following Details action in SampleController:
public ActionResult Details(int sampleNumber)
{
var sample = (Sample)Session["sample"];
if (sample == null)
{
var pallet = (Pallet)Session["pallet"];
sample = pallet.Samples.First(s
=> s.SampleNo.Equals(sampleNumber));
if (sample.Defects.Count < 1) // Postback issue?
{
var access = new Access();
sample.Defects = access.GetDefects(pallet.Grv.GRVNo,
pallet.PalletSeq, sampleNumber);
sample.GetImagePaths();
sample.Pallet = pallet;
Session["sample"] = sample;
}
}
return View(sample);
}
And this Update action:
public ActionResult Update(IEnumerable<HttpPostedFileBase> files, Sample sample)
{
var pallet = (Pallet)Session["pallet"];
sample.Pallet = pallet;
sample.SaveImages(files);
access.UpdateSample(sample);
access.UpdateDefects(sample);
Session["sample"] = sample;
return View("Details", sample);
}
I am trying to debug an issue, but somehow the line return View("Details", sample); is not calling the above Details action (the breakpoint does not stop the code).
It does return a view of the selected sample, but none of the operations present in Details are occurring.
I tried changing the return statement to
return View("Details", sample.SampleNo);
To match the signature of Details, but then I get :
The model item passed into the dictionary is of type 'System.Int32', but this dictionary requires a model item of type 'MVCQCPage.Models.Sample'.
How is that possible? the Details action does NOT ask for a Sample param, so why does this not just return Details and pass in the sampleNo (int) value?
Note that the above Details action is the only method of that name in SampleController.
You need to do RedirectToAction
public ActionResult Update(IEnumerable<HttpPostedFileBase> files, Sample sample)
{
var pallet = (Pallet)Session["pallet"];
sample.Pallet = pallet;
sample.SaveImages(files);
access.UpdateSample(sample);
access.UpdateDefects(sample);
Session["sample"] = sample;
return RedirectToAction("Details", sample.SampleNo);
}
Please check https://msdn.microsoft.com/en-us/library/system.web.mvc.controller.redirecttoaction(v=vs.118).aspx
As the other answers mention, I need to use RedirectToAction.
However, i also need to pass in a named sampleNumber parameter:
return RedirectToAction("Details", new { #sampleNumber = sample.SampleNo });
You need to use RedirectToAction:
public ActionResult Update(IEnumerable<HttpPostedFileBase> files, Sample sample)
{
var pallet = (Pallet)Session["pallet"];
sample.Pallet = pallet;
sample.SaveImages(files);
access.UpdateSample(sample);
access.UpdateDefects(sample);
Session["sample"] = sample;
return RedirectToAction("Details", sample);
}
The View() method returns the specified view without invoking the Details Action, however the RedirectToAction() method redirects to the specified action not the View().
Related
I return IActionResult with the value of an anonymous object from a controller method.
How do I get the data out again?
Here is the controller method reduced to the very problem:
[HttpPost]
public async Task<IActionResult> UploadFile(IFormFile file)
{
long size = file.Length;
return Ok(new { count = 1, size });
}
Then I get the .Value property, which is an object and make it dynamic.
But no, it is not recognised.
[Fact]
public async Task UploadFileTest()
{
// # Given.
var formFile = new Mock<IFormFile>();
var sut = new HomeController(null);
// # When.
IActionResult result = await sut.UploadFile(formFile.Object);
// # Then.
OkObjectResult okResult = (OkObjectResult)result; // Ok.
dynamic dynValue = okResult.Value; // Ok.
var count = dynValue.count; // Not ok.
}
Expected result:
count should be 1.
Actual result:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException : 'object' does not contain a definition for 'count'
Try reflection:
var count = (int)okResult.Value.GetType().GetProperty("count").GetValue(okResult.Value);
If your test and original code not in the same project, If that so, you need to create AssemblyInfo.cs in your original code project to make it (original) share the dynamic object structure.
you could see more at https://sodocumentation.net/csharp/topic/4264/assemblyinfo-cs-examples#-internalsvisibleto-
It shall has code like below (Replaced the string as your test name)
[assembly: InternalsVisibleTo("MyAssembly.UnitTests")]
I created a view to display data from the database
public ActionResult UpdateDetail()
{
using (UsersDatabaseEntities ude = new UsersDatabaseEntities())
{
ude.Configuration.ValidateOnSaveEnabled = false;
return View(ude.Users.Where(a => a.Email == User.Identity.Name).FirstOrDefault());
}
}
Then, I tried to edit and save to database
[HttpPost]
public ActionResult UpdateDetail([Bind(Exclude = "IsEmailVerified,ActivationCode")] User user)
{
if (ModelState.IsValid)
{
using (UsersDatabaseEntities ude = new UsersDatabaseEntities())
{
ude.Entry(user).State = EntityState.Modified;
ude.SaveChanges();
}
}
return View(user);
}
The problem is, it seems to not be saved to the database. i tried to call UpdateDetail again, and it shows the data do not saved.
I do not find syntax error.
does the data truly saved?
Add the debugger inside your UpdateDetail action and also write the if(ModelState.IsValid){ //..logic goes there } else{ return View();} check the modelstate don't have any error then try to update User and seem like it's happen when we have some non-nullable field and passing to the null values into them.
I have a problem transfering data from one view to another via the controler actions.
I the first view is a grid.mvc-Grid displayed. By select on row of the grid I get the ID for that object.
by transfering this to an action in the controler I try to filter the data. That works fine.
Here is the filter:
[HttpGet]
public ActionResult PersonenById(int id)
{
var personen = new ObservableCollection<Person>();
//Getting the data here :-)
foreach (DataRow r in access.Rows)
{
Person p = new Person();
//do some stuff
personen.Add(p);
}
//return PartialView("Personen", personen); //does not work
TempData["personen"] = personen;
return RedirectToAction("Personen"); // redirect to another view
}
In method II the view is filled:
public ActionResult Personen()
{
var persons = new ObservableCollection<Person>();
if (TempData["Persons"] == null)
{
}
return View(persons); //Works perfect
}
else
{
persons = (ObservableCollection<Person>) TempData["Persons"];
return View(persons);//does not redirect to that View
}
}
(Sorry for the strange formating. :-))
Is there any different way to send data from a view to another?
I tried:
return partial;
return View("Persons",persons);
and a lot other stuff.
You can redirect in a .cshtml view.
Eg:
Context.Response.StatusCode = 403;
Context.Response.Redirect(
$"{Context.Request.PathBase}/Error/403", false);
Should work like this:
return RedirectToAction("Personen", model);
Also, the "Personen" action should have the model as an argument, like this:
public ActionResult Personen(Person model) ...
LE: I have also noticed you have tried to send the data through the TempData object. Make sure the indexed object's name is the same (e.g. TempData["person"] everywhere)
Hope it answers your question.
This is now fixed. A combination of Ish's suggestion below plus adding calls to #HiddenFor in the view resolved the problem.
I have an ASP.NET MVC 5 web application where users can mark a defect as resolved. I want to display a list of potentially related defects, with check-boxes that users can tick to indicate that yes, this is the same defect, and should also be marked as resolved.
So I have a View Model with a property that is a collection, each member of which contains a defect object property and Boolean IsSameDefect property. This all works fine in the GET action method and in the view. I can display the related defects and tick the boxes.
The problem arises in the POST action when I want to update the data. At this point the property (the collection of potentially related defects) is null. I'm having a hard time trying to figure out how to pass this data back to the controller?
Code as requested ...
// GET: /DefectResolution/Create
public ActionResult Create(int ciid)
{
int companyId = User.CompanyID();
DefectResolutionCreateViewModel drcvm = new DefectResolutionCreateViewModel(ciid, companyId);
return View(drcvm);
}
// POST: /DefectResolution/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(DefectResolutionCreateViewModel drcvm)
{
DefectResolutions currentResolution = drcvm.DefectResolution;
currentResolution.CreatedOn = System.DateTime.Now;
currentResolution.UserID = User.UserID();
if (ModelState.IsValid)
{
unitOfWork.DefectResolutionRepository.Insert(currentResolution);
if (currentResolution.ResolutionStatusID == 2)
{
//code breaks here as drcvm.RelatedUnresolvedDefects is null
foreach (var relatedDefect in drcvm.RelatedUnresolvedDefects)
{
if (relatedDefect.IsSameDefect)
{
DefectResolutions relatedResolution = new DefectResolutions();
relatedResolution.ChecklistID = relatedDefect.RelatedChecklist.ChecklistID;
relatedResolution.CreatedOn = System.DateTime.Now;
relatedResolution.ResolutionNote = currentResolution.ResolutionNote;
relatedResolution.ResolutionStatusID = currentResolution.ResolutionStatusID;
relatedResolution.UserID = User.UserID();
}
}
}
unitOfWork.Save();
return RedirectToAction("Index", new { ciid = currentResolution.ChecklistID });
}
return View(drcvm);
}
In the view ...
#model Blah.ViewModels.DefectResolution.DefectResolutionCreateViewModel
#{
ViewBag.Title = "Create Defect Resolution";
var relatedDefects = Model.RelatedUnresolvedDefects;
}
... and later in the view ...
#for (int i = 0; i < relatedDefects.Count(); i++ )
{
<tr>
<td>
#Html.EditorFor(x => relatedDefects[i].IsSameDefect)
</td>
</tr>
}
I followed Ish's suggestion below, and modified the code to refer to Model.RelatedUnresolvedDefects directly instead of using a variable as I had been doing. This does get me a bit further. The view model's RelatedUnresolvedDefects property is no longer null. But only RelatedUnresolvedDefects.IsSameDefect has a value. RelatedUnresolvedDefects.RelatedChecklist is null. Here's the controller code again showing where it now breaks ...
// POST: /DefectResolution/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(DefectResolutionCreateViewModel drcvm)
{
DefectResolutions currentResolution = drcvm.DefectResolution;
currentResolution.CreatedOn = System.DateTime.Now;
currentResolution.UserID = User.UserID();
if (ModelState.IsValid)
{
unitOfWork.DefectResolutionRepository.Insert(currentResolution);
if (currentResolution.ResolutionStatusID == 2)
{
//prior to change, code used to break here
foreach (var relatedDefect in drcvm.RelatedUnresolvedDefects)
{
if (relatedDefect.IsSameDefect)
{
DefectResolutions relatedResolution = new DefectResolutions();
//code now breaks here because relatedDefect.RelatedChecklist is null
relatedResolution.ChecklistID = relatedDefect.RelatedChecklist.ChecklistID;
relatedResolution.CreatedOn = System.DateTime.Now;
relatedResolution.ResolutionNote = currentResolution.ResolutionNote;
relatedResolution.ResolutionStatusID = currentResolution.ResolutionStatusID;
relatedResolution.UserID = User.UserID();
}
}
}
unitOfWork.Save();
return RedirectToAction("Index", new { ciid = currentResolution.ChecklistID });
}
return View(drcvm);
}
Without knowing your code.I suggest you to use for loop instead of foreach while rendering the defects in View (.cshtml).
Editing Answer based on your code.
Following statement in the view creating problem
var relatedDefects = Model.RelatedUnresolvedDefects;
You should directly iterate over the Model.RelatedUnresolvedDefects property in the loop.
#for (int i = 0; i < Model.RelatedUnresolvedDefects.Count(); i++ )
{
<tr>
<td>
#Html.EditorFor(x => Model.RelatedUnresolvedDefects[i].IsSameDefect)
</td>
</tr>
}
I have this action:
public ActionResult Report(AdminReportRequest reportRequest, FormCollection formVariables)
{
AdminEngine re = new AdminEngine();
AdminReport report = re.GetCompleteAdminReport(reportRequest);
return View(report);
}
I was wondering how could I go about Redirecting to another action within the same controller, passing the AdminReportRequest, and FormCollection variables?
I had something like this in mind:
public ActionResult EarningsSalesReport(AdminReportRequest reportRequest, FormCollection formVariables)
{
if (!reportRequest.Download)
{
AdminEngine re = new AdminEngine();
AdminReport report = re.GetCompleteAdminReport(reportRequest);
return View(report);
}
return RedirectToAction("ExcelSalesReport", reportRequest, formVariables);
}
public FileResult ExcelSalesReport(AdminReportRequest reportRequest, FormCollection formVariables)
{
AdminEngine re = new AdminEngine();
Stream SalesReport = re.GetExcelAdminReport(reportRequest);
return new FileStreamResult(SalesReport, "application/ms-excel")
{
FileDownloadName = "SalesReport" + DateTime.Now.ToString("MMMM d, yyy") + ".xls"
};
}
This is obviously wrong and throws up some errors, such as:
'System.Web.Mvc.Controller.RedirectToAction(string,
string,
System.Web.Routing.RouteValueDictionary)'
has some invalid arguments
and
Argument 3: cannot convert from
'System.Web.Mvc.FormCollection' to
'System.Web.Routing.RouteValueDictionary'
If someone could point me in the right direction I'd greatly appreciate it, I think I might have to edit the Global.asax file however I'm not overly familiar with this.
thanks.
You can use the TempData object.
The TempData property value is stored in session state. Any action method that is called after the TempDataDictionary value is set can get values from the object and then process or display them. The value of TempData persists until it is read or until the session times out.
This MSDN article explains it all.
public ActionResult EarningsSalesReport(AdminReportRequest reportRequest, FormCollection formVariables)
{
//...
TempData["Report"] = reportRequest; //store to TempData
//...
}
public FileResult ExcelSalesReport(AdminReportRequest reportRequest, FormCollection formVariables)
{
//...
var report = TempData["Report"] as AdminReportRequest;
//...
}