I am using Tuple
to pass two models inside the view like code given below.
@model Tuple<AdvanceSearchModel, List<SearchUserModel>>
<form role="search" method="post" action="/Public/AdvanceSearch">
<div class="form-group">
<label>Name</label>
<input name="FullNames" type="text" class="form-control" value=""/>
</div>
<div class="form-group">
<label>Product</label>
<input name="Products" type="text" class="form-control" value="" />
</div>
<div class="form-group">
<label>Location:</label>
<input name="Location" type="text" class="form-control" value="" />
</div>
<div class="form-group">
<label>State</label>
<input name="States" type="text" class="form-control" value="" />
</div>
<div class="form-group">
<label>Country</label>
<input name="Countries" type="text" class="form-control" value=""/>
</div>
</form>
All the name
attributes inside inputs are of AdvanceSearchModel
. How do I use tag helper such as asp-for
when passing multiple model to the views containing one or multiple forms? Also how do I retain values of the form after submitting the form in above scenario?
As you can see in the source code of InputTagHelper
You can see it creates the name
attribute based on the (lambda) expression in html-tag:asp-for
.
You need a form name
tag like this SearchUserModel[0].Location
Where:
SearchUserModel
is the property name on the model which is in the controller method you post to[0]
is the index in the listLocation
is the property on the iten in the list the SearchUserModel
instanceSearchUserModel
+ a prefix (like an int for which row in the list it is for example: usermodel[1]
)@model SearchUserModel
<input asp-for="Location" my-prefix="ListItem[@Model.Id]" class="form-control" />
SearchUserModel
part of the form should look.Why do i say this? It is easier to debug if you get weird databindings in your controller.
That said, option 1 is perfectly fine but it might lead to problems later, as it is very static template, you wont be able to add or remove rows easily.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With