PartialView renders new page instead of in div - asp.net-mvc

I had this problem before and can't for life of me remember how to fix it
or where I googled about it.
I am passing back from an action a partial view
return PartialView("CommentsListControl", recipe.Comments);
But this loads a new web page and not into my div WHY?????????
<table width="100%">
<tr><td>
<div id="divComments">
<% using (Ajax.BeginForm("CreateComment", new AjaxOptions { UpdateTargetId = "divCommentsList", InsertionMode = InsertionMode.Replace }))
{ %>
<textarea id="comments" name="comments" rows="3" cols="60"></textarea>
<table width="100%"><tr><td align="right">
Save Comment
<%= Html.Hidden("recipeid", Model.RecipeID) %>
</td></tr>
</table>
<% } %>
</div>
</td></tr>
<tr><td>
<div id="divCommentsList">
<% Html.RenderPartial("CommentsListControl", Model.Comments); %>
</div>
</td></tr>
</table>

The sample you showed doesn't have a submit button on the form. In WebForms, you could use an "asp:Hyperlink" control to generate a link that looked liked it submitted a form, but that was because WebForms was implementing the JavaScript for you. In MVC you have to do that yourself. Try swapping your "Save Comment" link out for a submit button:
<input type="submit" value="Save Comment" />
Then use CSS to style the button to your hearts content.

Related

The page is validated automatically after I set a validator and every time I want to redirect to another page, I get the error from validator?

I though I have finished working on the page, however, after another round of checking, it seems that the previous working link is not working anymore and every time when clicked, tries to validate the page.
The link is a part of a main master page.
Looks like the Post Back event called from the clicking on the link is trying to validate Date Pickers again.
I have the following logic to build CustomValidator and RadGrid, where I validates RadDatePickers:
<telerik:RadAjaxPanel ID="RadAjaxPanel1" runat="server">
<div align="center">
<asp:CustomValidator ID="checkForTwoDates" ClientValidationFunction="AtLeastOneDate_ClientValidate" ViewStateMode="Enabled"
EnableClientScript="True" ErrorMessage="At least one date should be selected" runat="server"></asp:CustomValidator>
<table class="moss2Search">
<tr>
<td>
<div runat="server">
<telerik:RadDatePicker RenderMode="Lightweight" ID="RadDatePicker1" width="100%" runat="server" DateInput-Label="Boarding Start Date"></telerik:RadDatePicker>
</div>
</td>
<td>
<div runat="server">
<telerik:RadDatePicker RenderMode="Lightweight" ID="RadDatePicker2" width="100%" runat="server" DateInput-Label="Boarding End Date">
</telerik:RadDatePicker>
</div>
</td>
<td>
<div>
<asp:CheckBox ID="chkMerActive" runat="server" Checked="true"/>Active
</div>
</td>
<td>
<telerik:RadButton RenderMode="Lightweight" runat="server" Text="Search" ID="Button1" OnClick="btnSearch_Click"></telerik:RadButton>
</td>
</tr>
</table>
</div>
</telerik:RadAjaxPanel>
<asp:Label ID="lblMsg" ForeColor="red" runat="server"></asp:Label>
<br />
<telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
<AjaxSettings>
<telerik:AjaxSetting AjaxControlID="ajaxControl">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="ajaxUpdateControl"></telerik:AjaxUpdatedControl>
</UpdatedControls>
</telerik:AjaxSetting>
</AjaxSettings>
</telerik:RadAjaxManager>
<div>
<telerik:RadGrid RenderMode="Lightweight" runat="server" ID="grdMoss2Merchants" AllowPaging="True" AllowSorting="true" PagerStyle-AlwaysVisible="true" OnNeedDataSource="BindToDatasource">
<ExportSettings IgnorePaging="true">
<Excel WorksheetName="Moss2Merchants" />
</ExportSettings>
</telerik:RadGrid>
</div>
What am I doing wrong or missing?

getting form value into controller in ruby on rails

I have a "search" page that have some controls and below is the search page code:
<%= form_for :search, :url => { :method => :get, :action => :search } do |f| %>
<table>
<tr>
<td align="center" style="vertical-align:top;">
<h2 style="color:Black; font-size: x-large;">Specs</h2>
<table>
<tr>
<td align="center">
<input type="text" name="tf_Zip" Style="text-align: left;" BackColor="#e5e5e5" Width="180px" ForeColor="Gray" Font-Size="Large">
</td>
</tr>
</table>
<table>
<tr>
<td>
<div class="button">
<input type="submit" name="search" value="Search" class="buttonSearch">
</div>
</td>
</tr>
</table>
</td>
<td align="center" style="vertical-align:top;">
<h2 style="color:Black; font-size: x-large;">
Result
</h2>
<% #user_zip.each do |uzr_zip| %>
<h1><%= uzr_zip.First_Name %></h1>
<% end %>
<table id="searchResult" width="100%" runat="server">
<tr>
<td bgcolor="#CCDBE0">
Image:
</td>
<td bgcolor="#CCDBE0">
<%= f.label(:zip, "Mentor") %>
</td>
</tr>
</table>
</td>
</tr>
</table>
<% end %>
And when I am trying to get the textbox value into the controllers page like below
def search
#students=Students.all
#blah = params[:search][:tf_Zip]
end
render 'search'
end
Then it gave me an error below, at this line #blah = params[:search][:tf_Zip]
undefined method `[]' for nil:NilClass
Kindle help me. Thanks
I think your params[:search] is nil?
so for this you can use
#blah = params[:tf_Zip]
or change your input field like this
<%= f.text_field :tf_Zip %>
or you can use like this
<input type="text" name="search[tf_Zip]" Style="text-align: left;"
BackColor="#e5e5e5" Width="180px" ForeColor="Gray" Font-Size="Large">
Look at your log: you will see what is coming through in params, then you'll see why params[:search][:tf_Zip] doesn't work.
The error is telling you, effectively, that params[:search] is nil, and that you can't call [tf_Zip] on nil.
Your problem is this line:
<input type="text" name="tf_Zip" Style="text-align: left;" BackColor="#e5e5e5" Width="180px" ForeColor="Gray" Font-Size="Large">
It will populate params[:tf_Zip] because it's name is "tf_Zip". If you want it to populate params[:search][:tf_Zip] then you should set the name attribute to search[tf_Zip].
What would be nicer though is to use the rails form field helpers. I don't know why you have so much raw html inside a form_for.
<input type="text" name="tf_Zip" Style="text-align: left;" BackColor="#e5e5e5" Width="180px" ForeColor="Gray" Font-Size="Large">
can be replaced with
<%= f.text_field :tf_Zip %>
which will populate params[:search][:tf_Zip]
For the other attributes (Style etc) you should set these with css. Rails will probably put a class on the field automatically which you can use to do this. The "style" (note lowercase) attribute can be used instead but it's clumsy as it doesn't allow you to restyle the field (and more generally, your site) with css.
Have you checked to see if params[:search] is nil? If it is, then trying to pull [:tf_Zip] will cause that error.
Usually you'd submit the form from one controller action and handle the result in another action.
And your statement end looks misplaced.

ASP.NET MVC - Ajax.BeginForm vs Ajax.ActionLink

I've noticed when I use Ajax.BeginForm (with a submit button) that the Model is passed to the Controller but when I use an Ajax.ActionLink it is not passed - or at least I have not discovered how to tap into it.
First question: How do you determine which is the better route to take?
Now, for a little deeper dive into one sample scenario: I have a model that has a couple dozen simple data type properties and a few List properties. The Create/Edit View is rendered with a Html.BeginForm(). The submit button returns the entire view model and I can then go through it and save all the data to the DB via the DB Model. Like I said I have a few List pieces as well. For example, I have a List of credit cards accepted which I render as a series of check boxes, a List of Services Provided also rendered as a list of check boxes. All of these are easy to deal with on the Form Post. However, I have one List that is basically a free-form text entry and I would like to have the textbox with an Add button followed by all of the List items, each with a delete button.
My Create/Edit view looks something like:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<RainWorx.FrameWorx.MVC.ViewModels.DirectoryEdit>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<div class="Column12">
<div class="Shadow"></div>
<h2 class="h2row"><%= Model.PageTitle%></h2>
</div>
<% using (Html.BeginForm())
{%>
<%: Html.ValidationSummary(true)%>
<fieldset>
<%--<legend>Fields</legend>--%>
<div class="editor-label">
<%: Html.LabelFor(model => model.Name)%>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Name, new { style = "width:20em;" })%>
<%: Html.ValidationMessageFor(model => model.Name)%>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Address1)%>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Address1, new { style = "width:20em;" })%>
<%: Html.ValidationMessageFor(model => model.Address1)%>
</div>
...
<div class="editor-label">
<%: Html.LabelFor(model => model.LookupAccepts)%>
</div>
<div class="editor-field">
<hr />
<% foreach (var a in Model.MyAccepts)
{
if (a.Checked)
{ %>
<input type="checkbox" name="AcceptIDs" checked="checked" value="<%: a.ID %>" /> <%: a.Name%><br />
<% }
else
{ %>
<input type="checkbox" name="AcceptIDs" value="<%: a.ID %>" /> <%: a.Name%><br />
<% }
} %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.LookupServices)%>
</div>
<div class="editor-field">
<hr />
<% foreach (var a in Model.MyServices)
{
if (a.Checked)
{ %>
<input type="checkbox" name="ServiceIDs" checked="checked" value="<%: a.ID %>" /> <%: a.Name%><br />
<% }
else
{ %>
<input type="checkbox" name="ServiceIDs" value="<%: a.ID %>" /> <%: a.Name%><br />
<% }
} %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.MyLicenses)%>
</div>
<div class="editor-field">
<hr />
<% Html.RenderPartial("EditLicense", model: Model); %>
</div>
<div class="editor-label">
</div>
<div class="editor-field">
<input type="submit" value="Save" />
</div>
</fieldset>
<% } %>
</div>
<div>
<%: Html.ActionLink("Back to List", "Index")%>
</div>
</asp:Content>
The Partial View looks like:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%# Import namespace="RainWorx.FrameWorx.MVC" %>
<% foreach (var license in Model.MyLicenses) { %>
<% } %>
My thought train for setting it up this way is pretty straight forward (I think it is anyways). I only have something to Ajax on the License section. I want to add one string after another without losing all of the information for the main model. And after the add is done I want to update the partial view with the updated List for it.
There is probably a better way to do this than what I laid out and if so, lay it on me. The main part I am trying to figure out right now (and quickly) is whether Ajax.ActionLink (and my Extension methods for it) are the right direction to go.
An Ajax.BeginForm simply ajaxifies the given form and when you submit it the values of all input fields that it contains are sent to the server. The only difference with a normal form is that the they are sent using AJAX.
On the other hand Ajax.ActionLink generates a simple anchor tag which performs an AJAX request to the given url. It won't send any additional values to the server unless you specify it.
How do you determine which is the better route to take?
Personally I don't use any of those. I use standard Html.BeginForm and Html.ActionLink and write the code manually to AJAXify them unobtrusively with jQuery (if I need to use Ajax of course).
For your scenario of implementing dynamic list editing you may take a look at the following blog post.

Postback problem with partial view

I have the following view
<form action="/Questionnaire/Submit" method="post">
<%:"UserName : "%>
<%=ViewData["UserName"]%>
<%=Html.TextBox("test",ViewData["tt"])%>
<p />
<%:"Phone Number :"%>
<%=ViewData["PhoneNumber"]%>
<p />
<%
foreach (var q in Model)
{
Html.RenderPartial("Question", q);
}
%>
<input type="submit" name="submit" value="submit" />
</form>
That render the following partial view
<% using (Html.BeginForm("Submit", "Questionnaire", FormMethod.Post))
{%>
<%:"Question Number "%>
<%=Model.QuestionNumber%>
<%:" "%>
<%=Model.Body%>
<%:" "%>
<%
foreach (var option in Model.Options)
{%>
<p/>
<%=Html.RadioButton(option.QuestionId.ToString(), (option.IsSelected) )%> <%= option.OptionBody%>
<%
}
}
%>
The problem is , The form dosn't submit , and when I remove the "foreach" statment from the master view, It works
My Objective, is to have the updated model ( from the master view and partial view ) to save it later on in DB
Your master view contains a form and then your partial view also creates a form each time it renders, so you will be left with a page containing multiple forms but only one 'Submit' button.
I'm not 100% certain what you need to do, but I'd try and remove the 'BeginForm' call from the partial and see if that fixes the problem.

Model Binding issue

Using Asp.net MVC, I have one view that's strongly bind to "List" , and loop to partial render a partial view as following
<%
foreach (var q in Model)
{
Html.RenderPartial("Question", q);
}
%>
and this partial view strongly bind to "Question"
<%
foreach (var option in Model.Options)
{
%>
<p/>
<%=Html.RadioButton(option.QuestionId.ToString(), (option.IsSelected) )%> <%= option.OptionBody%>
<%
}
}
%>
Do a post back in the master view as following
<% Html.BeginForm("Submit", "Questionnaire", FormMethod.Post); %>
<input type="submit" name="submit" value="submit" />
<% Html.EndForm(); %>
Finally at my controller
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Submit(List<Question> test)
{
var x = test;
return View("Submit");
}
My Form is
<% Html.BeginForm(); %>
<%:"UserName : "%>
<%=ViewData["UserName"]%>
<%=Html.TextBox("test",ViewData["tt"])%>
<p />
<%:"Phone Number :"%>
<%=ViewData["PhoneNumber"]%>
<p />
<%
foreach (var q in Model)
{
Html.RenderPartial("Question", q);
}
%>
<% Html.EndForm(); %>
<% Html.BeginForm("Submit", "Questionnaire", FormMethod.Post); %>
<% TempData["form"] = ViewData;%>
<input type="submit" name="submit" value="submit" />
<% Html.EndForm(); %>
and when I include all in one begin form, post back dosn't fire"
My Question is
Why x is always null?
and how can I get my updated model ( having user selection to the radio button rendered in the partial view )
Should I use TempData to store my value? and how ?
After receiving the right updated model, i will save it to DB.
Thanks!
<% Html.BeginForm("Submit", "Questionnaire", FormMethod.Post); %>
<input type="submit" name="submit" value="submit" />
<% Html.EndForm(); %>
It looks to me like your form only has a single input inside of it. You need to render your editors inside the form, or they will not get included as part of the POST.
Update
So now you've got two forms: one has the inputs you want to submit and the other one has the actual button. The problem is, clicking the submit button on the second one will only submit the second (practically empty) form. Why not combine your forms?
<% Html.BeginForm("Submit", "Questionnaire", FormMethod.Post); %>
<%=ViewData["UserName"]%>
<%=Html.TextBox("test",ViewData["tt"])%>
<p />
<%:"Phone Number :"%>
<%=ViewData["PhoneNumber"]%>
<p />
<%
foreach (var q in Model)
{
Html.RenderPartial("Question", q);
}
%>
<input type="submit" name="submit" value="submit" />
<% Html.EndForm(); %>

Resources