ASP MVC form fields wrongly getting filled in - asp.net

I have an ASP MVC form which updates a business object and then its action goes to another page.
Both of these pages have a textarea with an id "Description", when it goes to the other page, for reasons unknown it fills in the values which were entered before the form submission.

Which method are you using to update your object? Have you tried adding the object type name in the ID?
Like, Product.Description instead of just Description.

Not knowing what your doing exactly, I'd say there is a 'Description' property on the ViewData's Model. ASP.NET MVC will try to match up your form values with values from the ViewData, including the Model. You can rename the control or reference the object directly as Chad said. eg Product.Description
With more details, someone may be able to help more.

Without seeing your controller actions this is a bit of guess work but how does it go to another page?
If in the post controller you return a different view (or return another action that returns a view etc) it may be that the ModelState is using the attempted values from the previous form submission, this is the expected behaviour (it's how the validation system and model binding works for a start). If you are sending them to a different form use a RedirectResult to your next action and use TempData if needed for any state you need to keep while transitioning.
If at all possible I'd also recommend using the strongly typed helpers such as Html.TextAreaFor(x=>x.Description)

<p><label>Description</label><br /><br /><%=Html.TextArea("Description", new { style = "width:470px;" })%></p>
For clarification, I'm going from an "Edit project" page, to a "Add issue" page

Related

How do I check if a ASP.NET MVC view contains a form?

I need to change the page formatting depending on whether the page (actually a layout template) contains a form or not. Is there any way to determine this in ASP.NET MVC 4?
If I understand you well, you can use ViewBag or ViewData or Session.
For example when you page was loading, you can set ViewBag data in get method. Then you can decide in view page what you want.
Also another way, when your page loaded in javascript you can post info of your page has form. You can collect this data in Session with a special key for current page.
As a result yes we can, I hope my answer enough. If not enough, I can help you if you explain well what you want really?

Many forms for editing entity on one page

I have a list of objects on my page. I need to edit an object in a popup.
There are many objects, and generating many forms for each object is not correct.
What can I do, can an iframe do in a popup?
You don't need to use iframe.
Build your form in your controller and render it in the html. The fact that it's in popup doesn't change anything. It doesn't matter if it's in popup or not, the final result will be POST call to your action.
You should have an action that renders a form, callable from an ajax in the view where you have all those entities.
Just change the entity id that will be received in the action as argument by changing the ajax url using js depending on the clicked entity you want to edit.
Then return with the ajax the form already rendered so with only one form you can edit as many entities (of the same class) as you want, without even need to render one form before they click which one they want to edit.

Get values of hidden fields in controller (ASP.NET MVC)

Can I reach the value of hidden fields in controller action ? And how ? Do I put it in the model somehow ?
Thank you
EDIT: some code example how to store something in hidden field and retrieve it on postback would be appreciated.
Hidden fields are just like any other form fields when you get to the POST action being sent to the server. There's nothing that makes them special outside of their display (or lack thereof, rather) in the browser. Thus, they should be accessible in much the same way you currently access other form fields.
How do you currently access other form fields? If they're just mapped to a model which is being passed into the controller action, then hidden fields should be mappable just the same.
Conversely, you should be able to manually access any HTTP POST data from HttpContext and the Request object.

MVC2 - How put common LOGIC control (Like Search/Find) on each page?

I'm having trouble figuring out how to do the following:
On every page (or every page I so desire), I'd like to put a common control widget (e.g. think - Search functionality that contains a textbox+button). What's the best way to do this, and who handles the submit button (assuming it is a submit button)?
i.e. what does my ViewUserControl look like? Does it have a form? does it use jQuery onclick""? Does it post to the main View's action method, or can I redirect it to another Controller/Action?
I have tried using RenderAction of a "Search.ascx" which contains a Form, and is handled by my SearchController... but in the SearchController, it then tries to call RedirectToAction... and I get a complaint about RedirectActions not allowed on Child Actions.
I'm a bit lost on what to do next, so suggestions greatly welcome!
Ray
You seem to be on the right track (using ViewUserControl and RenderPartial). But with the information you have provided, it is not easy to see what you other problems are (RenderAction , ...)
It is easy:
Create a UserControl (.ascx) and get a form in there with URL being /search/..., something that you can get back.
In your views, call RenderPartial and provided the view name
Create your controller to receive the post from your search. This is not the same controller as your parent view controller.
The best way to have the HTML elements show up is to put them in a master page, or in a partial that is referenced by your master page. I would have it be it's own form and submit to your SearchController.
Let me know if you want more particulars.
RenderPartial is the way to go here. Your control.ascx will consist of it's form and submit button.
What's the best way to do this
Probably a partial view. An .ascx file.
and who handles the submit button
(assuming it is a submit button)?
The partial view
what does my ViewUserControl look
like? Does it have a form?
Yes, it has a form. It should be as self contained as possible.
does it use jQuery onclick""? Does it
post to the main View's action method,
or can I redirect it to another
Controller/Action?
Well, whatever fits your exact scenario best. It should probably post to whichever action is most appropriate. That is not likely to be the main view's action, since the partial is reused in different parent views.
I have tried using RenderAction of a
"Search.ascx" which contains a Form,
and is handled by my
SearchController... but in the
SearchController, it then tries to
call RedirectToAction... and I get a
complaint about RedirectActions not
allowed on Child Actions.
You'll probably want to render it using RenderPartial in the parent view:
<%: Html.RenderPartial("MyPartialView.ascx") %>
Ok, I figured out what my problem was. The replies above are correct. I have my own Search.ascx user control and SearchController, I also used RenderPartial, but what stumped me was that I forgot to explicitly specify the controller/action... so then I was fiddling around with onclick events and Url.Action on my button instead.
<% using (Html.BeginForm("MySearchAction", "MySearchController")) { %>
Thanks to all who replied.

asp.net mvc client side validation on multiple forms in one page

I have 4 forms in my asp.net mvc view. I have enabled client side validation on each by put <% Html.EnableClientValidation(); %> Above Html.BeginForm() of each form. The issue is that regardless of the fact that I've specified ID's for the forms the first form on the page gets validated whenever I click submit of the other forms.
Is this usage supported or am I doing something wrong?
this may help
<%=Html.ValidationMessageFor(m => ((RegistrationFormModel)m.Data).Email, null, new { id = "registration_Email" })%>
Make sure you have validation messages for the properties. Unless you have a validation message or (ValidateFor()), the property isn't added to the set of elements validated on form submission.
See this question for more info.
MVC2 fully supports the setup that you're looking for, my guess is that you're applying this to something like displaying a registration form and a login form on the same page?
If so you just need each form to have independent property names, i.e.
LoginModel would have a Username property and RegistrationModel would have a RegistrationUsername.
Not a great example there, but what's probably happening is that the validation is firing cross form because your properties have the same name.

Resources