MVC - Receive Form with Viewbag Variable from database and passing to the view - asp.net

i have a html form into my database.
In this form i have a value which contains a Viewbag,
for example value="#Viewbag.MyVariable"
In my view when i try to receive my form all works fine,
but my problem is that the Viewbag value are not converting
to the value which comes from my controller.
any ideia how to resolve this ?
my code:
View:
#Html.Raw(p.Form)
My form in my database looks like:
<form action="/MyController/MyAction" method="post">
<input type="hidden" name="num" value="#ViewBag.num" />
....
....
....
</form>
What i have try to do was with:
Stringbuilder a=new Stringbuilder;
#Html.Raw(a.To.String())

Your code should technically work, if you are using MVC you could try
#MvcHtmlString.Create(p.Form);

I found a solution and now i solved it.
what i have done:
in my form the only part which are different from the others
forms are the dropdownlists.
i only put the part from <select> to </select> in my
database. (only the dynamically part of my form)
in my view i have write my form and only insert the variable
which comes from my controller to insert the data from my
database on the right part in my form.
here is the code:
<form action="/MyController/MyAction" method="post">
<input type="hidden" name="num" value="#ViewBag.num" />
#MvcHtmlString.Create(p.Form);
<button type="submit" name="submit" id="submit">OK</button>
</form>
So i have never more the problem with the variables to be
converted, because the variables are still the same

Related

form submit redirect to a new url with AMP-HTML

I have a simple problem yet it seems impossible to solve in AMP!!
I have a simple form with an input and a submit button like this:
<form id="myform">
<input type="text" id="srchInput"/>
<button type="submit">Submit</button>
</form>
All I want is to be able to concat a static url to the input value and redirect the page to the result, when the form is submitted.
For instance if the user inputs: "Hello" and submits the form, I would like to redirect him to a page like "MY/STATIC/URL/Hello".
Is there any way to achieve this in amp?
One way of doing this is by setting the AMP-Redirect-To header in the response (See AMP-form-redirection). Send the user input on form submission, and then generate your desired url from your API endpoint and set AMP-Redirect-To header in your response to the generated URL.
Another way of doing it would be by using navigateTo(url=STRING) action (See AMP Actions & Events) on for the form submit event. In this case you have to store the value in input to a state by using events like input-throttled, and then use url substitution in the navigateTo url string to append amp-state value.
The first method is guaranteed to work.
The second method should work in theory, but I was unable to figure out how to get AMP-STATE value by url substitution. The code for the same should be something like:
<form id="myform" on="submit:AMP.navigateTo(url="MY/STATIC/URL/AMP_STATE(endValue)>")">
<input type="text" id="srchInput" on="input-throttled:AMP.setState({ endValue : event.value })" />
<button type="submit"> Submit </button>
</form>
If you can figure out how to substitute amp-state value to the url this should work. Let me know if it helped.
The easiest way to do this is via a GET action:
<head>
...
<script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>
</head>
<body>
<form method="GET" action="MY/STATIC/URL" target="_top">
<input type="text" id="srchInput" name="srcInput" value="test">
<button type="submit">Submit</button>
</form>
</body>
The form submit will navigate to /MY/STATIC/URL?srcInput=test.

ASP.NET Core using two models in single form

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.
what you need
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 list
Location is the property on the iten in the list the SearchUserModel instance
My suggestion
Not to do
Extend the InputTagHelper and add a prefix option (which adds a prefex to the name).
Use a view model Not a tuple!
Create a partial view that only takes SearchUserModel + a prefix (like an int for which row in the list it is for example: usermodel[1])
In your view loop over the list and call the partial.
result
#model SearchUserModel
<input asp-for="Location" my-prefix="ListItem[#Model.Id]" class="form-control" />
Better longterm option
Make a HTML template on how SearchUserModel part of the form should look.
Do ajax call to get the data or put the data as json in your view. (or take step 3 from what not to do)
Generate the form with well structured javascript.
On submit Instead of submitting the form, parse the from to json and send this as json ajax call.
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.
References for proper html name tags for lists:
http://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx
How does MVC 4 List Model Binding work?

Ljava.lang.String is displayed when I accesed param in thymeleaf Spring

I have a problem while accessing the page parameter in thymeleaf.
Please see my below code :
<input type="hidden" name="resetKey" th:value="${param.key}"/>
I am trying to access parameter key from query string and send it as hidden parameter to the server.
After page loads, it is displayed like below :
<input type="hidden" name="key" value="[Ljava.lang.String;#b1fd0f9">
I want to send the key back to the server when the form is submitted.
You are trying to print a multi-value, a String array. To print the first value:
<input type="hidden" name="resetKey" th:value="${param.key[0]}"/>
Or this should work too:
<input type="hidden" name="resetKey" th:value="${#request.getParameter('key')}"/>
Source: Spring MVC and Thymeleaf: how to access data from templates

form verification before submitting

I have following form
<form id="myForm" action="/Problems/Post" method="post" enctype="multipart/form-data">
<input type="text" id="problemSubject" name="problemSubject" />
<input type="file" id="uploadFile" name="uploadFile"/>
<textarea rows="" cols="" class="form-textarea" id="problemDescription" name="problemDescription"></textarea>
</form>
I have to submit form to controller method(which i have done), but it should be first validated i.e. it should not contain empty fields. What i want is that "a message should appear telling that field is blank". How this can be done. Please help me. Thanks.
Take a look at some of the samples. (http://www.asp.net/mvc/tutorials/older-versions/javascript/creating-a-mvc-3-application-with-razor-and-unobtrusive-javascript)
Essentially you can validate on client side and on server side, which you should do both.
It is very easy to do validation in asp.net mvc3. Look at some tutorials like above.
Or this one: http://www.codeproject.com/Articles/249452/ASP-NET-MVC3-Validation-Basic
u can use server side validation controls...
plz hv a look [link]http://msdn.microsoft.com/en-us/library/aa479013.aspx
or use the jquery gven below--->
function callOnload(){
if($('#problemSubject').val() == '')
alert('fill the values');
if($('#uploadFile').val() == '')
alert('fill the values');
}

how can the Controller detect changes to child entities/rows in a View?

What's the most appropriate approach to the following scenario?
I have two models:
TimeSheet: Consisting of four properties: DayStartTime, DayEndTime, BreakTime, List<TimeSheetHours>
TimeSheetHours: Consisting of four properties: ClientId, ProjectId, HoursWorked
My time sheet view is strongly typed to a IEnumerable<TimeSheet> list. This list contains 7 TimeSheet models, one for each day of the week Sunday-Saturday. This time sheet view uses an editor tempate to produce input fields for DayStartTime, DayEndTime, BreakTime, for each of the days of week:
Each day contains a section for clients & projects. Clicking an "add" button fires an ajax function which calls a controller action to render a partial view dynamically inserting a row of controls (Client combobox, Project combobox, HoursWorked textbox) into the appropriate clients & projects section for that day:
I'm having a hard time understanding how I can update each timesheet model's List<TimeSheetHours> when I add a new client/project row via the ajax call.
So in the end, when I submit to the controller for inserting the timesheet, I have everything thing I need for each timesheet model in the list I produced the view.
Any assistance would be much appreciated.
Thank you!
You will either have to massage the names of these elements before they're posted to look like how I have it below or in your ajax call pass in the index of the TimeSheet and then create the name:
TimeSheetHours Index = 0
<input type="text" id="TimeSheet[0]_TimeSheetHours[0]_DayStartTime"
name="TimeSheet[0].TimeSheetHours[0].DayStartTime" value="1" />
<input type="text" id="TimeSheet[0]_TimeSheetHours[0]_DayEndTime"
name="TimeSheet[0].TimeSheetHours[0].DayEndTime" value="2" />
<input type="text" id="TimeSheet[0]_TimeSheetHours[0]_BreakTime"
name="TimeSheet[0].TimeSheetHours[0].BreakTime" value="1" />
TimeSheetHours Index = 1
<input type="text" id="TimeSheet[0]_TimeSheetHours[1]_DayStartTime"
name="TimeSheet[0].TimeSheetHours[1].DayStartTime" value="3" />
<input type="text" id="TimeSheet[0]_TimeSheetHours[1]_DayEndTime"
name="TimeSheet[0].TimeSheetHours[1].DayEndTime" value="4" />
<input type="text" id="TimeSheet[0]_TimeSheetHours[1]_BreakTime"
name="TimeSheet[0].TimeSheetHours[1].BreakTime" value="0" />
You're probably much better off intercepting the submit event and iterating over each TimeSheetHours html-represented element and assigning the name as a mask and then submitting the form. This should allow the model binder to reconstruct your List<TimeSheet> with a populated List<TimeSheetHours> property.
Here's a good tutorial on how to solve this issue of posting a collection: ASP.NET MVC 2 Model Binding for a Collection

Resources