I have this function located in a controller called AccountCodes.
[ValidateAntiForgeryToken]
[HttpPost]
public async Task<ActionResult> Process()
{
JsonResult result = new JsonResult(null);
List<string> uploadResults = new List<string>();
return Ok(uploadResults);
}
My intention is to use dropzone to push files to this function. When I try to load the url in the razor view in a variable like this:
var url = "#Url.Action("Process", "AccountCodes",new { area="Configurations"})";
However, when I load the page and look at the console, the url is always "". Am I missing something?
EDIT:
This is where I put the javascript code:
$(document).ready(function () {
var url = "#Url.Action("Process", "AccountCodes",new { area="Configurations"})";
$("#reset").attr("disabled", true);
});
Your code is correct. It may because that you have some wrong global configuration.
Please verify the following things:
Step 1: Check your application
Use MVC in your start up:
// Startup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
// Other middlewares.
app.UseMvcWithDefaultRoute();
}
Step 2: Check your controller
Make sure there is a controller named AccountCodesController.
Make sure AccountCodesController extends Controller.
Check your routes. If you specify [Route("Something")] to your controller, make sure it is absolutely correct.
Step 3: Try adding a route to your action.
Adding an attribute [Route('SomeAction')] to your action Process might solves this.
Step 4: Check your area existance
Please make sure your controller is located in correct area as this document mentioned: https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/areas?view=aspnetcore-2.2#areas-for-controllers-with-views .
namespace MVCareas.Areas.Products.Controllers
{
[Area("Products")]
public class ManageController : Controller
Related
i want to set variable (someFlag = false/true) in JS so i can disable/enable javascript function.
I would like to do it with afterStarted(options) [described in link below].
https://learn.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/?view=aspnetcore-6.0#javascript-initializers
The question is:
Is there any way to get the data from appsettings.json to function afterStarted(...). Either through options parameter or something else, doesn't matter.
Thanks.
Maybe inject IConfiguration into the blazor page?
#inject IConfiguration _configuration
protected override afterStarted(){
var somevariable = _configuration["othervariable"];
}
Failing that, I create an object like ApplicationState with an interface, make that a scoped service in Program.cs, define the object's properties, then inject that interface into your blazor page:
Program.cs
builder.Services.AddScoped<IApplicationState, ApplicationState>();
var applicationState = app.Services.GetRequiredService<IApplicationState>();
applicationState.someVar = builder.Configuration["SomeVar"];
blazor
#inject IApplicationState _applicationState
protected override afterStarted(){
var somevariable = _applicationState.someVar;
}
Hope this helps
In order to consolidate some bulky code in a controller I have been able to do this inside function
public function myCustomFunction(Request $request){
include(app_path() . '/myFunctions/myCustomFunction1.php');
}
It does successfully work but by doing the include, I noticed it doesnt carry the namespace and/or other variables set in the controller.
If its just a simple model it needs I was able to add
Use \App\myModel;
to the top of the controller and it runs the code just fine as if its part of the controller.
What I am having issue with is when I want to run validation. I cant figure out what to "use" to work on the included page.
If I run my logic in the controller without doing include it works, but if put inside the include it does not, and I get NO errors either.
I tried this inside the include page to activate the validation from within
the include file
namespace App\Http\Controllers;
use Validator;
use Request;
which mimics the original controller, but still does not work. Is what I'm trying to do possible but I'm doing something wrong? Or am I doing something that will never work? If all else fails I can keep the code within the actual controller to make it work, but I'm trying to accomplish some sort of clarity on complex controllers. Such as...
public function myCustomFunction(Request $request){
if(request('value1')=='1'){
include(app_path() . '/myFunctions/myCustomFunction1.php');
}else{
include(app_path() . '/myFunctions/myCustomFunction2.php');
}
}
This structure is already working on anything that doesn't need validation so I'm hoping there is a simple way to hook the include page into the same set of validation tools the original controller has access to.
In the controller, so include the files incorrectly. It's best for you to create a Helpers folder in the app folder, in it create classes with your functions.
namespace App\Helpers;
class HelpersOne {
public function myFunctionOne(){.../*youre code*/}
public function myFunctionTwo(){.../*youre code*/}
}
And already in your controller you can call these classes.
use App\Helpers\HelpersOne;
...
public function myCustomFunction(Request $request){
if(request('value1')=='1'){
$myFunction = new HelpersOne();
$myFunction->myFunctionOne();
}else{
$myFunction = new HelpersTwo();
$myFunction->myFunctionTwo();
}
}
So I have model entity Place that has to have initialized some relations.
Now I am using autocreated model form-backing bean in controller as follows:
#GetMapping(value = "/add")
public String addPlacePage(final Place place, Model model) {
model.addAttribute("services", serviceRepository.findAll())
.addAttribute("categories", categoryRepository.findAll())
.addAttribute("cities", cityRepository.findAll());
return "admin/place/addPlace";
}
Place place is created my Spring (Spring-MVC probably). Is there a way to provide a factory method for backing bean? I would like to not to do this in controller.
I have tried to put factory code in my configuration like this:
#Bean
#Scope(value = "prototype")
public Place place() {
log.info("Creating place in FACTORY");
Place place = EntityFactory.emptyPlace();
return place;
}
but this does not work.
So I have ask the same question in #spring channel on freenode, and this is something that came up thanks to user <#_ollie>. In order to do what I want, #ModelAttribute method should be defined in controller like this:
#ModelAttribute
private Place emptyPlace(){
log.info("Creating place in FACTORY");
Place place = EntityFactory.emptyPlace();
return place;
}
and related docs are here
https://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#mvc-ann-modelattrib-methods
Problem Statement:View not able to load on ajax success method.
Description:
I'm having couple of dropdowns as cascaded(second binding based on the value of first).
On change of the first dropdown I'm binding second dropdown and again on change of the second dropdown,now on right side I want to display a list of record based on the two dropdown values.
For this,I'm using the following JS code in onchange event for the dropdown 2:
function ShowDocsList() {
var teamId = $('#TeamID').val();
var projectId = $("#ProjectID").val();
var Url = "#Url.Content("~/DocsHome/DocsList")";
$.ajax({
url: Url,
type:'POST',
dataType: 'html',
data: { TeamID: teamId ,ProjectID : projectId},
success: function (data) {
return data;
$('.docs-detail').html(data);
}
});
Here,in DocsHome Controller,DocsList method is getting hit on change of second dropdown which is project dropdown.But the view is not getting rendered .Following is my Controller Code:
public ActionResult DocsList(int teamId, int projectId)
{
List<CustomerViewModel> customerViewsModels = SmartAdminHelper.GetCustomers(db1);
if (Request.IsAjaxRequest())
return PartialView("DocsList");
else
return View("DocsList");
}
Again ,I'm getting record in List but while debugging it does not pass to the DocsList view which according to me is correct.
Here DocsList is the view I want to render as a partial view on change of the second dropdown.
According to my knowledge,while debugging it comes to the point return PartialView("DocsList") but then again it goes back to the ajax success method and finally I find that there I'm doing something wrong.
Earlier I have Json to get data but here I'm calling actionmethod from ajax. So, not sure that also might be a problem as I'm new to this.
What and where exactly I'm doing wrong?
Saroj, I see that this is an old question and that you needed to get it done quickly, but if you happend to come back to this, I'll add my two cents. You need to remove the return statement that David and Ehsan mention above. The rest of the callback function does what it should. In your action method it doesn't look like you're doing anything with the parameters you pass in. I'm assuming that you are going to figure that out after you get the view down to the client. So, lets get that view down to the client.
I like to pass the rendered partial view back to the client as a string of HTML. I do this using a method that I keep in a controller base class that each of my controllers inherit from. To use the method you will need to reference System.Web.Mvc and System.IO namespaces.
The method looks like this:
private string RenderViewToString( string viewName, object model ) {
ViewData.Model = model;
using ( var sw = new StringWriter() ) {
var viewResult = ViewEngines.Engines.FindPartialView( ControllerContext, viewName );
var viewContext = new ViewContext( ControllerContext, viewResult.View, ViewData, TempData, sw );
viewResult.View.Render( viewContext, sw );
viewResult.ViewEngine.ReleaseView( ControllerContext, viewResult.View );
return sw.GetStringBuilder().ToString();
}
}
You pass your model and the name of the view to the method and it returns the rendered view HTML as a string which you can return to the client as a ContentResult.
Update your action method like so:
public ActionResult DocsList(int teamId, int projectId)
{
List<CustomerViewModel> customerViewsModels = SmartAdminHelper.GetCustomers(db1);
if (Request.IsAjaxRequest())
var viewContents = RenderViewToString("DocsList", customerViewsModels);
return Content(viewContents);
else
return View("DocsList");
}
Assuming that the element that you want the rendered view to appear in has the css class '.docs-detail' on it you'll be in business. Hope this helps!
I am trying to call a method inside a controller in MVC from a javascript action. The javascript action is supposed to invoke this method inside the controller and send some parameters to it.
My Javascript code looks like this:
location.href = '#Url.Content("~/Areas/MyArea/MyMethod/"+Model.MyId)';
My Method is defined as follows:
[HttpGet]
public ActionResult MyMethod(int? MyId)
{
doSomething(MyId);
return View("MyView");
}
However, when i debug the application, when the method is called the MyId parameter is passed as null and not as the current value of the MyId parameter in my model. What can I do to correctly send or retrieve this value? Thanks!
In your route definition I suppose that the parameter is called {id} and not {MyId}:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"MyArea_default",
"MyArea/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
So try to be more consistent and adapt your controller action parameter name accordingly:
[HttpGet]
public ActionResult MyMethod(int? id)
{
doSomething(id);
return View("MyView");
}
Also you probably wanna use url helpers instead of hardcoding some url patterns in your javascript code:
window.location.href = '#Url.Action("MyMethod", "SomeControllerName", new { area = "MyArea", id = Model.MyId })';
The Url.Content helper is used to reference static resources in your site such as javascript, css and image files. For controller actions it's much better to use the Url.Action helper method.