sitefinity parameters ActionResult method - sitefinity

I have widget where I like to call the a custom ActionResult method? can I do that in the
widget properties?
public string ActionResultName { get; set; }
public ActionResult Edit()

The Edit method will be automatically called when you visit the /yourPage/edit url.
or if you are inside another action method, like Index, you can do RedirectToAction("Edit").

Related

How to pass FormFile and json object in single model using Asp.net core

I have a model class. That class contains multiple properties , list of custom class and list of formfile data. I need to create post request for this model using Asp.net Core.
Model Class
public class TenoModel
{
[Key]
public int Id { get; set; }
public DateTime Date { get; set; }
public float Salary { get; set; }
[Required]
public string User { get; set; }
public List<IformFile> files {get;set;}
public List<Employee> employees{get;set;}
}
Custom class for employee
public class Employee
{
public int Id {get;set;}
public string Name {get;set;}
public double Salary {get;set;}
}
HttpPost request:
[HttpPost("CreateTeno")]
[Consumes('multipart/form-data']
public async Task<IActionResult> CreateTeno([FormFile] TenoModel model)
{
..... save the model data to teno database
}
When I execute the HttpPost request on swagger Formfile data and some property data come to incoming request on CreateTeno method. I fill the Employee object also in swagger. but that data not coming in my CreateTeno method. List of Employee data count zero. How to pass model class with multiple properties, List of IformFile and multiple list customclass in HTTP post request. Give a sample example.
You'll have to change the signature of your controller method like this:
public async Task<IActionResult> CreateTeno([FromForm] TenoModel model)
Tested with Postman like this
Please note that you have a syntax error here: [Consumes('multipart/form-data'], should be [Consumes("multipart/form-data")]

Asp.Net Core - multiple action methods with the same name and different parameters

I'm looking for a way to have more than one action method with the same name in controller without changing Url (route).
[HTTPPost]
Public ActionResult Method1 (Dto1 param)
{
}
[HTTPPost]
Public ActionResult Method2 (Dto2 param)
{
}
[HTTPPost]
Public ActionResult Method3 (Dto3 param)
{
}
This throws error -
Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException: The request matched multiple endpoints
Dto1, Dto2 and Dto3 derive from a base Dto, each have properties specific to different request methods. I am trying to avoid having a single method with a common Dto which will require multiple validations such as validating mandatory fields based on the value of other fields, etc. If we can have 3 different Post methods with different Dtos, it would makes things much easier
Adding Dtos (Simplified)
public class BaseDto
{
public string CommonProp1 { get; set; }
public string CommonProp2 { get; set; }
}
public class Dto1: BaseDto
{
public enumType Type = enumType.Type1;
public string Property1 { get; set; }
}
public class Dto2 : BaseDto
{
public enumType Type = enumType.Type2;
public string Property2 { get; set; }
}
public class Dto3 : BaseDto
{
public enumType Type = enumType.Type3;
public string Property3 { get; set; }
}
You can use Routes or calling a private method from the three above methods, you shouldn't do this as you want. I think your problem is more deep.
But.... if you still want it, here is a workaround.
Instead of receiving an object, receive a string with json content and parse the object.
But you will have to have a property inside the "json object" or another parameter that defines you wich object it is (Dto1, Dto2 or Dto3). In any case will be the same that use different routes or methods because objects are different.
[HTTPPost]
Public ActionResult Method (string param)
{
//Decode your string called param with JSON with a property inside
}
or
[HTTPPost]
Public ActionResult Method (string param, int type)
{
//Decode your string called param with JSON switching "type" as 1, 2 or 3
}
UPDATE after your update:
I suggest you receive BaseDto and the type in other parameter.
[HTTPPost]
Public ActionResult Method (BaseDto param, int type)
{
}

AspCore Multiple Post Actions with Same Action Name

I have a checkout page that has multiple methods for payment. Each method has its own partial view containing its own model. I am trying to get keep the url the same on each of the different methods so if there is an error the url doesn't change. Is there a way to achieve this? Thank you for any help, I have been mulling over this for a while now.
CheckOut Model
public class CheckoutForm
{
public Method1Form method1Form { get; set; }
public Method2Form method2Form { get; set; }
public Method3Form method3Form { get; set; }
}
CheckOut Controller
[HttpGet]
[Route("checkout/{guid}")]
public IActionResult Checkout([FromRoute] String guid)
{
....
return View(model);
}
[HttpPost]
[Route("checkout/{guid}")]
public IActionResult Checkout([FromRoute] String guid, Method1 model)
{
....
//Some Error Condition Triggered
return View(checkoutmodel);
}
[HttpPost]
[Route("checkout/{guid}")]
public IActionResult Checkout([FromRoute] String guid, Method2 model)
{
....
//Some Error Condition Triggered
return View(checkoutmodel);
}
[HttpPost]
[Route("checkout/{guid}")]
public IActionResult Checkout([FromRoute] String guid, Method3 model)
{
....
//Some Error Condition Triggered
return View(checkoutmodel);
}
Similar Question without an answer https://stackoverflow.com/questions/42644136
You cannot. There is no way for Route Engine to differentiate those 3 post methods.
You could append something at the end to the URL to make them different.
[HttpPost]
[Route("checkout/{guid}/paypal")]
public IActionResult Checkout([FromRoute] String guid, Method1 model)
{
....
}
[HttpPost]
[Route("checkout/{guid}/authorizenet")]
public IActionResult Checkout([FromRoute] String guid, Method2 model)
{
....
}

ASP.NET MVC4 Model Not Binding

I have a very simple ASP.NET MVC4 page. It's rendering an edit form for the CustomerModel. The form displays correctly, but when I hit edit and post back, the model isn't being bound. Instead, all the properties of the CustomerModel are left at their defaults. Note that the correct controller method is being invoked, so that's not the issue.
I can see the form values with matching names to the model properties (Id, Name, Description), but the model doesn't have them set.
Ideas?
Here is the model:
public class CustomerModel
{
[Required]
public Guid Id;
[Required]
public string Name;
[Required]
public string Description;
}
And here is the relevant controller method:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(CustomerModel customerModel)
{
if (ModelState.IsValid)
{
//...Do stuff
return RedirectToAction("Index");
}
return View(customerModel);
}
Finally, here is a screen shot of the form collection with the populated values:
Your model has public fields but not public properties, these are not the same.
Change to:
public class CustomerModel
{
[Required]
public Guid Id {get; set;}
[Required]
public string Name {get; set;}
[Required]
public string Description {get; set;}
}
The default MVC model binder will work with properties, not fields.
More about this here - http://rightbrainleft.net/2011/02/default-mvc-model-binder-doesnt-like-fields/

How to AccessSimpleMemberShipprovider properties of current user ASP.net MVC4

I have a razor .cshtml view in MVC4 and I want to print a field of my userprofile class. I understand I can get the Username from User.Identity.Name but I don't know how to access the other properties of the currently logged in user in the partial. Do I need to do this in my controller and reference it in my view? If so is there a controller that is available to all views so I am not setting this variable in every controller? Is there a simple way to access the current users properties as defined in the UserProfile model class right in the view? To give some context. I have my model as...
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
//Added to round out requirements for site user profile
public string FirstName { get; set; }
public string LastName { get; set; }
public string Site { get; set; }
}
I need to access the FirstName in the view. If I get it in the controller then I will need to do it in all controllers since this is a page partial for all pages.
I decided to put it in a session variable since it is not critical, sensitive, or effects the stability of the application. In my controller on login or registration I set the session variable with...
var context = new UsersContext();
var curuser = context.UserProfiles.First(p => p.UserName == model.UserName);
Session["FName"] = curuser.FirstName;
I got this from Pro ASP.NET MVC and from this post. And in my Razor view I use this derived from this post.
#HttpContext.Current.Session["FName"].ToString()
In the controller action that is serving the partial view put FirstName in the ViewBag.
If there are more properties that you would like to return then create a ViewModel for them and make the controller action return that.