Multiple field searches in MVC 3 using C # - c#

Hi I am relatively new to MVC 3. i was just wondering if there is a way to do multiple field searches.
I'm looking to have many textboxes on a page, where the user can input search criteria such as Region, Salesperson Rating, Salesperson Subject, etc .
and then when the user clicks the search button it should populate a list with Salespeople matching the criteria entered.
I have been looking on the web and i haven't found a concrete answer...
Thank you in advance...

You could use a grid like jqgrid to get these kinds of functionalities. It's relatively easy to implement in asp.net mvc. I once wrote a mvc helper for jqgrid, it's built for mvc2 but can also be used in mvc3 as well. There's also a sample app for the helper, you can find it here to see if fits your needs.

Yes, you can have multiple parameters passed to a controller action, e.g. by defining a form with multiple input fields in your view and let the form post to the action url. The data can be passed using multiple method parameters, a custom model class that defines the relevant properties, or simply by using a form collection.

Related

ASP.NET Core Web App - Dynamic form values passed to OnPost

I'm creating an ASP.NET Core Web App with .NET 6 and I'm trying to find a way to pass a dynamic list of variables back to the OnPost handler. Here's the details, I create a form on the page during the OnGet. I don’t know how many fields, or the types, this form will have at design time. It can be as few as one or as many as 20. They could be text fields, dates, integers, checkboxes, etc., but I don't know ahead of time. When the user submits this form, I need to collect all the values in the OnPost and display the results; the results are also dynamic but that's a separate issue. So how do I get the values? I can't create BindProperties or create a Model, ‘cause I don't know how many or what they are. I can easily assign a unique Id/Name to the form while I'm building it, Param1, Param2, Param3, etc. but that's not the important part. I can't imagine that I'm the first person to attempt this, but I can't find any examples of how to accomplish this, at least in Asp.Net Core .NET6
Since you aren't using model binding you can access the form data directly and loop through all the data
var formData = Request.Form;
foreach(var data in formData)
{
// Name attribute from the form
string keyName = data.Key;
// Value the user entered
string keyValue = data.Value;
}

How do I validate some fields some of the time?

I'm using C# in .Net-Core MVC and I have a form that users will need to fill out.
All of the fields that are shown on the page are required to be filled out. The issue I'm running into is that some of the fields on the form are hidden and others are displayed based on choices previously made on the form.
If I put the [Required] tag on all of the fields in the model, when I validate the ModelState, it flags the not displayed fields as invalid.
Is there a way that when I try to validate the ModelState, I can validate only the fields displayed on the page and ignore the fields that have been hidden?
Thanks.
Unfortunately the [Required] works globally in MVC.
You will need to develop your own validation attributes. Hopefully someone already did it but for MVC with .NET Framework (see the code here):
For validations that has the form of: “Validate this field only when
this other field has certain value”, I have coded 3 attributes:
RequiredIf, RangeIf and RegularExpressionIf that inherints from
ValidationAttribute.
Now you will need to translate it in order to work for .NET Core.
If you are looking for a more generic solution, the Web Forms framework has a very good concept of Validation group. It allows you to validate - or not - logically grouped properties.
If I put the [Required] tag on all of the fields in the model, when I
validate the ModelState, it flags the not displayed fields as invalid.
Of course cuz you set a parameter "Required". Disable that parameter from fields what can be not displayed or make nullable

asp.net mvc ways for generating dynamic form fields at runtime

this is the more or less the schema i want to generate my dynamic form on based on the fields above. i am going to add the direction , max size, default value and like wise some more fields in it. i am looking for recommended ways and methods in asp.net mvc for generating dynamic fields at runtime.
1) if i design my own engine for it then how? i am interested on that also but this is the last thing i am looking at. method to apply validation is very important in my scenario
2) any framework that may lessen the working time? or anything else?
I'll describe the generic approach, I don't want to code it for you.
Create meta class to describe each field (type, name, maxlength, null value handling, data source for combos, etc.)
Load the data from database and preprocess it
Populate the ViewBag with sanitized values
Create helper that will generated the control specific code
Html.ControlFor("Name", metadata);
Loop in view over the metadata collection.
which will generate textbox, combobox, etc.
Remeber that MVC form handling works over list of key-values, it's the Binder feature that converts it to objects. Saving data won't be difficult (dynamically created INSERT, UPDATE statement, ...).

Parsing what an automatically generated select element in ASPX is doing?

I'm trying to make sense of the following select element :
<select name="ctl00$ctl00$ContentPlaceHolder1$ContentItems$ddlResponseRange id="ctl00_ctl00_ContentPlaceHolder1_ContentItems_ddlResponseRange" class="Content" style="width:11%;">
This is all automatically generated code, correct( ie like ct100 ) ? What are the $ symbols doing - are they for expression bindings? FYI the ddl here means "drop-down list".
$ and _ are used as separators between the server-side ID of the control, ddlResponseRange, and the server-side IDs of its parent controls that are of type INamingContainer (typically <asp:content> controls).
A control by itself on a page, right inside a <form runat="server"> does not have attributes like that. But when you start using master pages, or data binding controls, then you are using controls that implement INamingContainer. This interface is like a marker, and it instructs the ASP.NET runtime to start adding the control's server-side ID to that of its child controls. This is necessary to guarantee unique name and ID attributes in the generated HTML.
Ultimately this infrastructure is what supports ASP.NET Web Forms post back mechanism and client-side scripting.
If you are using ASP.NET 4.0 or later, you can actually influence the way client-side IDs are generated by ASP.NET. See this article for different ways to configure the client-side IDs.
It's all just one big string. From the point of view of HTML, they're all valid characters to make up the value of an attribute, so in that sense it's "correct".
As for trying to use the id/name value yourself, or to rely on assumptions about the name/id based on that structure, I'd suggest avoiding that. I find it preferable to pretend that ASP will generate a random name/ID that I can't use, despite the fact that in reality it's just the IDs of all elements from that item up to the root. If you find yourself writing out ID values like that yourself, you're probably doing something wrong.
As for why it follows that convention instead of just actually assigning a random ID/Name (or none at all) is so that it can re-use it to correlate the HTML response to the structure of of items in memory. It's also important to ensure that IDs are unique per page, and by using this structure it means that APS ID values only need to be unique among sibling elements.

How to get all active parameters in ASP.NET MVC (2)

I was wondering whether there is a way to create an ActionLink or similar, that changes only a few parameters of the actual query, and keeps all the other parameters intact. For example if I'm on an URL like http://example.com/Posts/Index?Page=5&OrderBy=Name&OrderDesc=True I want to change only the Page, or OrderBy parameter and keep all other parameters the same, even those I don't yet know of (like when I want to add a Search parameter or something similar too).
The header of my current action looks like this:
public ActionResult Index(int? Page, string OrderBy, bool? Desc)
and I'm only interested in the values that this controller "eats". I want however that when I extend this action (for example with a string Search parameter) the links should work the same way as before.
Here is what I did already:
Create a new RouteValueDictionary and fill it with everything from RouteData.Values
Problem: This only fills the parameters that are used in the Routing, so all other optional parameters (like Page) to the controller are lost
Add everything from HttpContext.Request.QueryString to the previous dictionary
This is what I am currently using
Problem: It might have some junk stuff, that the Controller didn`t ask for, and it doesn't work if the page was loaded using POST. You also don't have any ModelBindings (but this isn't much of a problem, because we are re-sending everything anyway)
Use HttpContext.Request.Params
Problem: this has too much junk data which imho one shouldn't add to a RouteValueDictionary that is passed to an ActionLink
So the questions:
Is there an RVD that has all the data that was passed to the Controller and was used by it?
Is this solution good, or are there any caveats I didn't think about (mainly in the context of changing a few query parameters while keeping the others intact)?
Is there a way to filter out the "junk" data from the Params object?
EDIT: Checked the RouteData.DataTokens variable, but it's usually empty, and doesn't contain everything I need. It seems to only contain parameters that are needed for the routing somewhere, but not all of the parameters.
Have a look in RouteData.DataTokens.
RouteData.DataTokens # MSDN documentation:
Gets a collection of custom values that are passed to the route handler but are not used when ASP.NET routing determines whether the route matches a request.
HTHs,
Charles

Categories

Resources