views:

46

answers:

3

Hi There, May be I am getting the whole idea wrong but this is what I am facing as a horrible problem at this point. In my application (MVC2, ASP.NET) I have something like following :

<%using (Html.BeginForm("ProcessOrder", "ShoppingCart", FormMethod.Post, new { @name="processorder"}))
{ %>

other html codes. [typically a cart item list]

<%Html.RenderPartial("ProcessOrderPartial", Model); %>

<%} %> 

<a href="#" onclick="document.forms[0].submit();">
<img id="Img2" runat="server" src="~/App_Themes/DefaultTheme/images/btn_purchase_tickets.gif" width="149" height="25" />
</a>

Then in my controller I have defined my action like below:

[HttpPost]
public ActionResult ProcessOrder(HK7Mvc.Models.ShoppingCartViewModel dataModel)
{
   if (ModelState.IsValid)
   {
         ok everything looks good. now process the order.
         return Redirect("~/ShoppingCart");
   }
   return PartialView("ProcessOrderPartial",dataModel);
}

The prolem is: When partial view is working file with validation but the other html content of my page is not showing [the cart item list, page header logo, and other static/dynamic html bla bla].

How/why is this happening ? My understanding was partial view will only render the .ascx and simply put/write that where i have called it.

Please help. thanks in advance.

A: 

Your Controller's Action method is the reason for what is happening:

return PartialView("ProcessOrderPartial",dataModel);

You should be passing "ProcessOrder" instead of "ProcessOrderPartial". Your full View is never being called there.

Andrew Barber
Hi Andrew, Thanks for your ans. Now I'm not stuck at the page and can work further. However, I need to bug you more as the solution actually lead to an another issue. Kindly view the 2nd ans to this for my second newby ques:
Ali Nahid
A: 

Scenario: For the whole "shopping cart process" operation I have only one view "Views/ShoppingCart/Index.aspx". I am calling "OrderProcess" action by declaring a form in index.aspx like below :

<%using (Html.BeginForm("ProcessOrder", "ShoppingCart", FormMethod.Post, new { @name="processorder"}))
{ %>

   <html bla bla><text box, drop down etc>
   <submit link (not button as I had to use an image), document.forms[0].submit in its  onclick event>
<%} %>

Now, when submit link is clicked the form is actually posted to, in other words the url is Mvc/ShoppingCart/ProcessOrder whereas I do not have any .aspx for this. I issue occured with the absolute paths in for js links. In Index.aspx I have used jquery and json for some other purpose which I linked like below:

In the head content place holder of Index.aspx
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1.min-vsdoc.js" type="text/javascript"></script>
<script src="Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
<script language="javascript">
     $(document).ready(function() {
         $('#ddlPostage').change(function() {
             $.getJSON('ShoppingCart/PostageChanged',
            { postage: $(this).find('option').filter(':selected').text(), transactionFee: $('#txtTransactionFee').val(), goodsTotal: $('#txtGoodsTotal').val() },
            function(data) {
                //alert(data);
                $('#txtOrderTotal').val(data.ordertotal);
                $('#txtPostagePrice').val(data.postageprice);
            }
        );

         });
     });
</script> 

Thus when there's a url like "mvc/ShoppingCart/ProcessOrder", it's causing the above js script to fail. In my other action methods in the controller "ShoppingCartController" I managed to avoid this problem by return Redirect("~/ShoppingCart"); But for this case this way is not possible as I specifically need to return the invalid model for validation.

Questions: How to work around a problem/situation like this ? [May be I have got whole MVC concept wrong but hoping there always is a twiking].

Thanks in advance. (sorry for the wordy desc)

Ali Nahid
Please do not post new questions as 'answers' to your first question. This is not a 'forum'. If you have new questions, post new questions.
Andrew Barber
A: 

I think I have found a soln to this. The problem was in the $.getJSON('ShoppingCart/PostageChanged'), which was not correct for obvious reason [I now feel the dumbest on this planet]. Changed it to $.getJSON('<%=Url.Action("PostageChanged","ShoppingCart") %>' and things seems to be working as it supposed to.

However, My question still remains (asking it in a different words):

as I have done

<%using (Html.BeginForm("ProcessOrder", "ShoppingCart", FormMethod.Post, new { @name="processorder"})) 
{ %>

So I understand the actionMethod is ProcessOrder ans so is the URL MVC/ShoppingCart/OrderProcess.

I have also tried doing this:

<%using (Html.BeginForm()) 
    { %>

But is doesnt go to the desired actionmethod "OrderProcess" even though this method is the only one which is [HttpPost] in my controller. Also have tried creating an action method named "ShoppingCart" with [HttpPost], did not work either.

I ghosty thing is : I have an action method in my "HomeController" named Contact(Model) and another method Contact(). I have a view called Contact.aspx. The URL MVC/Contact works fine with a submit button in it with validation.

If this works then why not the ShoppingCart ???? :S

Ali Nahid
Please do not post new questions as 'answers' to your first question. This is not a 'forum'. If you have new questions, post new questions.
Andrew Barber