How do you initialize a FormCollection with properties in .NET? - c#

I've tried the usual way of:
var form = new FormCollection { "WeekList" = weekfilter, "PracticeList" = practicefilter}
and all possible deviations I could think of, but ultimately had to seperate it apart as:
var form = new FormCollection();
form["WeekList"] = weekfilter;
form["PracticeList"] = practicefilter;
How can I initialize this inline? Is it possible? (I'm basically trying to mimic a form being submitted)

If you're using ASP.NET MVC (not core), you can initialize System.Web.Mvc.FormCollection like this:
var form = new FormCollection {
{"WeekList", weekfilter},
{"PracticeList", practicefitler}
}
Demo in .NET Fiddle
But I'm not the right computer to test this. I'm basing this on the .Add method of FormCollection begin declared as:
public virtual void Add(
string name,
string value
)
What types are the filter variables declared as?

If you're using Microsoft.AspNetCore.Http.FormCollection in aspnet core, you can initialize like this:
var formCol = new FormCollection(new Dictionary<string, Microsoft.Extensions.Primitives.StringValues>
{
{ "Field1", "String Value 1" },
{ "Field2", "String Value 2" },
{ "Field3", "String Value 3" }
});
Demo in .NET Fiddle
Then, if you need to create a controller with test data, you can pass to the controller create method like this:
// Call create controller with test data
_controller.Create(formCol);

Related

How do i pass multiple parameters to c# method from browser URL

I ad trying to pass parameters to call the following C# method
public ActionResult GenerateInvoicePDFByInvoiceNum(string id,string apikey)
{
IInvoiceRepository rep = db.GetInvoiceRepository();
//
var api = Guid.Parse(apikey);
Invoice invoice = rep.GetByExpression(i => i.InvoiceNo.Equals(id) && i.Visit.Branch.Practice.APIKey.ToString().Equals(apikey)).FirstOrDefault();
if (invoice==null)
{
return HttpNotFound();
}
//return new Rotativa.ActionAsPdf("PrintInvoice", new { id = invoice.Id })
//{
// //CustomSwitches = "--load-error-handling ignore "
// CustomSwitches = "--disable-javascript"
//};
return RedirectToAction("GenerateInvoicePDF", new { id = invoice.Hash });
}
From the browser I am trying to call it with a call like this which worked when I had only one parameter, but I don't know how to change those to pass the second parameter
http://foobar.n.co.za/Newlook/PrintInvoice/GenerateInvoicePDFByInvoiceNum/72341d
Thanks
On your url: http://foobar.n.co.za/Newlook/PrintInvoice/GenerateInvoicePDFByInvoiceNum/72341d
"/72341d" represents an optional parameter.
Try to pass a query string.
To do that, you need to specify the parameter name and value you want to pass.
http://foobar.n.co.za/Newlook/PrintInvoice/GenerateInvoicePDFByInvoiceNum?id=72341d&apikey=YOUR_API_KEY
This is how you pass parameters by url:
http://foobar.n.co.za/Newlook/PrintInvoice/GenerateInvoicePDFByInvoiceNum?id=72341d&apikey=yourapikeygoeshere
You can pass multiple parameters as "?param1=value1&param2=value2"
http://foobar.n.co.za/Newlook/PrintInvoice/GenerateInvoicePDFByInvoiceNum?id=1&1pikey=2
You have three choices to do this:
Pass parameters using query string like /?id=value&apikey=value
Add a custom route in "RouteConfig" (~/App_Start/RouteConfig.cs) like below:
routes.MapRoute(
name: "RouteName",
url: "{controller}/{action}/{id}/{apikey}"
);
Use attribute routing. To do this first enable it by calling the "routes.MapMvcAttributeRoutes()" method in "RouteConfig" and then apply the [Route(urlPttern)] attribute to the related action and pass the url pattern to the method
after applying solution 2 or 3 you can pass parameters in url like:
"/foobar.n.co.za/Newlook/PrintInvoice/GenerateInvoicePDFByInvoiceNum/72341d/apikeyvalue"

Ranorex validate-How to check if RepoItemInfo object is equal to string data in C# code?

I just want to check if my RepoItemInfo object (which is username Joe McAdam btw) is equal to string data.
I just tracked this element in Chrome, stored it in my repository and it is a span with #innertext="JoeMcAdam"
Then I made code module for mapping these objects in C# code:
public RepoItemInfo LeftNameRepoItem {get {return _pageHome.Home.ImeLijevoInfo; }}
And I have prepared data Name in my context file for this LeftName to check if they are equal:
void ITestModule.Run()
{
var dto = new LoginDto () {
Name = "Joe McAdam",
WaitTimeLimit = 20000,
};
LoginDtoContext.template = dto;
}
I just need a proper code example for checking if they are eqal. What should I do? Do I have to make some adapter for this RepoItemInfo to convert it in string, text or something else?
I hope I provided enough details about my problem.
Thanks in advance!

Is it possible to bind list of strings to Action Result Include property

In in the snippet below
public async Task<ActionResult> CreateAsync([Bind(Include = DataRequestEditProperties)] ComplexModel complexModel)
{
if (ModelState.IsValid)
{
await DocumentDbRepository<ComplexModel>.CreateItemAsync(complexModel);
return RedirectToAction("Index");
}
return View(complexModel);
}
I am currently requesting specific JSON properties to be returned for creating a C# razor view the following way :
CreateAsync([Bind(Include = "list", "of", "about", "50", "strings")]
I'm wondering if it is possible to declare a list of strings somewhere else and referenced variable set somewhere else? Looking for a temporary solution as I am building a prototype and still defining the models of my MVC project. I would like to define what fields I am requesting in a separate file mapped to a variable that describes the query name and holds the list of strings i need to include
Example of declared variables
List<string> DataRequestEditProperties = new List<string> {"bunch", "o", "strings"};
List<string> DataRequestInformativeView = new List<string> {"infoFieldA", "infoFieldB","infoFieldC"};`

Proper way of passing additional view data to EditorFor

If I pass view data like this
#(Html.EditorFor(x => x.User2, "GenericList",
new ViewDataDictionary {
{ "ListType", ListType.CustomValidation },
{ "Param1", ObjectType.Asset },
{ "Param2", "User2" },
{ "DefaultValue", Model.User2 }
}
))
I am unable to access the Values via ViewData["ListType"]. The keys are stored in a property called Keys, and the values are stored in a ICollection property called Values
Passing the ViewData this way
#Html.EditorFor(x => x.User2, "GenericList",
new { ListType = ListType.CustomValidation,
Param1 = ObjectType.Asset,
Param2 = "User2",
DefaultValue = Model.User2
}
)
means I can get the values easily like ViewData["ListType"].
I don't liek the 2nd method of doing things because it's using anonymous types instead of a ViewDataDictionary. How can I use the first method but still call the ViewData as I expect to be,

Passing collection data to K2 custom service broker method

I have two K2 SmartObjects: SmartObjectA gets a list of objects. SmartObjectB is a custom SmartObject that executes a custom create method with the list returned from SmartObjectA (basic smart object) as input parameter.
In the custom service broker's code behind, I need to deserialize the list value.
How can I pass the list received in K2 as an input parameter for the create method? I am using a multivalue property and assigning SmartObjectA properties to it.
What format does K2 use to serialize multivalues and how can I check what the input value format is so that I can map the list correctly?
SmartObject A Properties:
Name - SOType.Text,
ID - SOType.Number,
Date - SOType.Date,
Description - SOType.Text
SmartObject B - Property to be populated with SmartObjectA's list:
[Property("LineItems", SoType.MultiValue, "LineItems", "The line items.")]
public string LineItems
{
get
{
return _lineItems;
}
set { _lineItems = value; }
}
Input properties and Create method for SmartObjectB:
[SourceCode.SmartObjects.Services.ServiceSDK.Attributes.Method("Create",
MethodType.Create,
"Create",
"Description",
new string[] { "CustomerId", "Date", "DueDate" },
new string[] { "CustomerId", "Date", "DueDate", "LineItems" },
new string[] { "TaxInvoiceId" })]
public TaxInvoice Create()
{
try
{
..deserialize here
}
}
Since you already have 2 different smartobjects, why not consider building a composite smartobject.
Using a composite smartobject, effectively doesnt require you to build a custom service broker.
Composite Smartobject Creation

Categories