Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,
I have a contact us page which has two parts
1. descriptive part
2. contact form
I made the contact form part as partial view.
But my problem here is that the action result for submiting data is not geting trigered.
code in partial view controller
C#
public class ContactUsFormController : Controller
    {
        //
        // GET: /ContactUs/
        SM_WEB.Models.SMWebDefaultConnection objSMWebDefaultConnection = new SM_WEB.Models.SMWebDefaultConnection();
        public ActionResult _ContactUs()
        {
            ContactUsForm contactUs = new ContactUsForm();
            return View(contactUs);
        }
        [HttpPost]
        public ActionResult _ContactUs(ContactUsForm candidateContactUs)
        {
            SM_WEB.Models.SMWebDefaultConnection objSMWebDefaultConnection = new SMWebDefaultConnection();
objSMWebDefaultConnection.smConn.spUNCandidateContactUs(candidateContactUs.unCandidateFirstName, candidateContactUs.unCandMobilePhone, candidateContactUs.unCandLandLine,
                candidateContactUs.unCandEmail, candidateContactUs.countryID, candidateContactUs.stateID, candidateContactUs.provinceID,
                candidateContactUs.unCandEnquiry);
            objSMWebDefaultConnection.smConn.SaveChanges();
            candidateContactUs = new ContactUsForm();
            return View(candidateContactUs);
        }


In Partial View
C#
@model SM_WEB.Models.ContactUsForm
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true);
    <fieldset style="margin: 20px">
        <legend>Contact Us</legend>
        <div class="editor-label">
            <label for="unCandidateFirstName">
                Name</label>
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.unCandidateFirstName)
            @Html.ValidationMessageFor(model => model.unCandidateFirstName)
        </div>
        <div class="editor-label">
            <label for="unCandMobilePhone">
                Mobile No</label>
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.unCandMobilePhone)
            @Html.ValidationMessageFor(model => model.unCandMobilePhone)
        </div>
        <div class="editor-label">
            <label for="unCandLandLine">
                Landline Number</label>
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.unCandLandLine)
            @Html.ValidationMessageFor(model => model.unCandLandLine)
        </div>
        <div class="editor-label">
            <label for="unCandEmail">
                Email</label>
        </div>
            <p>
            <input type="submit" value="Submit" />
        </p>
    </fieldset>
}


In Parent View

C#
@Html.Partial("~/Views/ContactUsForm/_ContactUs.cshtml",contact);


Any one help me please..
Posted
Updated 2-Jan-18 3:08am

Can you please try by specifying the action and controller explicitly in Html.BeginForm like below
C#
@using (Html.BeginForm("ActionName","ControllerName",FormMethod.Post)){


}

Hope this helps
 
Share this answer
 
Comments
Jineesh TR 7-Nov-14 0:08am    
Hi Jameel. It is working fine. But another problem arises. the parent view is not displaying. ie only the partial view is displaying.
Jameel VM 7-Nov-14 0:20am    
which is your parent view?
Jineesh TR 7-Nov-14 0:27am    
This is download main page. where it dispalys some static images and texts. here I am calling my partial view.

@{
ViewBag.Title = "Download";
Layout = "~/Views/Shared/_Layout.cshtml";
SM_WEB.Models.DownloadForm download = new SM_WEB.Models.DownloadForm();
}
<div class="row">
<div class="col-lg-6">
@Html.Partial("~/Views/DownloadForm/_DownloadForm.cshtml", download);
</div>
</div>
Jameel VM 7-Nov-14 0:52am    
what is the error showing?
Jineesh TR 7-Nov-14 1:00am    
Only the partial view part is displaying in the browser window. ie the parent page and its contents are not displaying.
Html.BeginForm does not support PartialViews and will allways update the whole view with the content of the partial view.

Use Ajax.BeginForm to achieve update of the partial view. Pass new AjaxOptions {updateTargetId='_partial' along with the call to specify the placeholder for the partial view.

In your case:
Ajax.BeginForm("_ContactUs", "ContactUsForm", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "contactUsPartial" })

<div id="contactUsPartial">
   @Html.Partial("~/Views/ContactUsForm/_ContactUs.cshtml",contact);
</div>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900