event.stop(e) and returnfalse statemens are not working - onsubmit

<div class = "heading">
Add Service Order
</div>
<%= link_to "Back" ,:action => "list"%>
<div style="float: left;">
<%#= form_tag ({:action => 'create' do %>
<%= form_tag({:action => 'create'}, :multipart=>true, :id=> #add_service_order, :method => :post) do %>
<%= render :partial => 'form' %>
<br>
<center><%= submit_tag 'Save',:class=>"buttons",:id => "save_button", :onClick => "po_num();"%></center>
</br>
<% end %>
</div>
<script>
function po_num(){
//alert ("11")
var po = document.getElementById('add_service_order_service_order_no').value
//alert(po);
//return false;
if (po == "")
{
alert("Please fill PO Number");
Event.stop(e);
}
}
</script>
event.stop(e) and Returnfalse are not working.Whenever i am going to save without filling mandatory fields it's giving alert but it is not stoping there and it is going to next page and saving..How can i resolve this ?

Related

How can I style simple form f.association check_boxes elements?

I'm trying to style a simple quiz. The image that I use is supposed be the background of the checkbox and the value.name should be on top (as a header) with the value.description as text below. Right now everything obviously is displayed next to each other. And I can't figure out a way to change it.
<%= simple_form_for #user_value, :method => 'post' do |f| %>
<%= f.error_notification %>
<%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>
<div class="form-inputs">
<%= f.association :value, label_method: lambda { |value| image_tag(cl_image_path value.photo.key) + "#{value.name} - #{value.description}"} , :label => "Select at least 10 values", as: :check_boxes, input_html: { class: "value-selector" }, item_wrapper_class: 'value-item'%>
</div>
<div class="form-actions">
<%= f.button :submit, "Continue" %>
</div>
<% end %>

select2 css takes too long to load

I'm using the nice Select2 for multiple select fields on my web applications search page. One can search for people by company/industry/school. Because there are hundreds of searchable values for each, it is taking time for the correct select2 css to load (about 1 second). The old ugly select fields can be seen in a flicker before the pretty select2 fields display. Attached are two screenshots that show the transition.
Here is my view code:
<%= form_tag('', :method => :get) do %>
<div class="row-fluid">
<div class="span4">
<label>What industries have they worked in?</label>
<%= select_tag "industry_ids", options_for_select((#visible_industries), params[:industry_ids]), { :placeholder => "Type to search...", :multiple => true, :id => "e3", :onchange => 'this.form.submit();' } %>
<%= hidden_field_tag :&, params[:industry_ids] %>
</div>
<div class="span4">
<label>What companies have they worked at?</label>
<%= select_tag "company_ids", options_for_select((#visible_companies), params[:company_ids]), { :placeholder => "Type to search...", :multiple => true, :onchange => 'this.form.submit();' } %>
<%= hidden_field_tag :&, params[:company_ids] %>
</div>
<div class="span4">
<label>Where did they go to school?</label>
<%= select_tag "school_ids", options_for_select((#visible_schools), params[:school_ids]), { :placeholder => "Type to search...", :multiple => true, :onchange => 'this.form.submit();' } %>
<%= hidden_field_tag :&, params[:school_ids] %>
</div>
<div class="row-fluid">
<% end %>
</div>
</div>
And controller code:
def people
#current_user = current_user
#visible_positions = Position.where{ is_visible.eq('true') }
#visible_educations = Education.where{ is_visible.eq('true') }
#visible_companies = #visible_positions.order("LOWER(company)").all.map { |p| [ p.company, p.company ] }.uniq
#visible_industries = #visible_positions.order("LOWER(industry)").all.map { |p| [ p.industry, p.industry ] }.uniq
#visible_schools = #visible_educations.order("LOWER(school)").all.map { |e| [ e.school, e.school ] }.uniq
#c = #visible_positions.where{company.in(my{params[:company_ids]})}.map(&:user_id)
#i = #visible_positions.where{industry.in(my{params[:industry_ids]})}.map(&:user_id)
#s = #visible_educations.where{school.in(my{params[:school_ids]})}.map(&:user_id)
unless #c.empty? && #i.empty? && #s.empty?
#users = User.find([#c,#i,#s].reject(&:empty?).reduce(:&))
end
end
Finally, I have this javascript in my assets directory (in addition to the select2 css):
$(document).ready(function(){
$('select').select2({
minimumInputLength: 1
});
});
What can I do to prevent this flicker from happening? Thank you.
I'll post my comment as an answer since it solved your issue.
My suggestion would be to hide the selects till the page is fully loaded (and select2 applied).
In the stylesheet, add a input {display:none;} rule to hide them (although visibility:hidden might be better). Then with jQuery override that rule $('input').css("display","inline");.

Form input in a foreach loop returns empty model

I have a list object for which I tried to display text boxes in a foreach loop. However the post returns empty object. I couldn't see the cause.
Here is the code in the view
<%using (Html.BeginForm("makeTransfer", "shareTransfer")) { %>
<% foreach (var i in Model.Inform)//int i = 0; i < Model.Inform.Count(); i++){ %>
<%:Html.HiddenFor(x=>i.shares, new{#value = i.shares}) %>
...
<td style = "width:20px"><%:Html.TextBoxFor(x=>i.sharesRq)%></td> cuddling
<%} %>
<%:Html.HiddenFor(x => x.accSrc, new { #value = Model.accSrc })%>
<%:Html.HiddenFor(x=>x.accDst, new{ #value = Model.accDst}) %>
Date of Transfer<%:Html.TextBoxFor(x => x.date)%>
Transfer with benefit<%:Html.CheckBoxFor(x => x.withBenefit)%>
<input type="submit" name="save" value="Save" /></div>
<input type="submit" name="cancel" value="Cancel" /></div>
<%} %>
And Here is the controller
public ActionResult makeTransfer(vmTransfer transfer, string save, string cancel)
{
if (cancel != null)
return RedirectToAction("startTransfer");
else if (save != null)
{
foreach (var t in transfer.Inform)
{ ...
My problem is, transfer.Inform( 2nd line from the last) which is a list is empty when the form posts. Any help please, ASAP.
I would recommend you using editor templates instead of writing any loops in your views:
<% using (Html.BeginForm("makeTransfer", "shareTransfer")) { %>
<%= Html.EditorFor(x => x.Inform) %>
<%= Html.HiddenFor(x => x.accSrc, new { #value = Model.accSrc }) %>
<%= Html.HiddenFor(x => x.accDst, new { #value = Model.accDst }) %>
Date of Transfer <%= Html.TextBoxFor(x => x.date) %>
Transfer with benefit <%= Html.CheckBoxFor(x => x.withBenefit) %>
<input type="submit" name="save" value="Save" /></div>
<input type="submit" name="cancel" value="Cancel" /></div>
<% } %>
and in the corresponding editor template (~/Views/Shared/EditorTemplates/InformViewModel.ascx):
<%# Control
Language="C#"
Inherits="System.Web.Mvc.ViewUserControl<AppName.Models.InformViewModel>"
%>
<%= Html.HiddenFor(x => x.shares) %>
...
<td style="width:20px">
<%= Html.TextBoxFor(x => x.sharesRq) %>
</td>
Remark: you might need to adjust the name of the editor template based on the type of the Inform property.
Editor templates will take care of generating proper id and names of the input fields so that everything binds correctly:
[HttpPost]
public ActionResult makeTransfer(vmTransfer transfer, string save, string cancel)
{
if (cancel != null)
{
return RedirectToAction("startTransfer");
}
else if (save != null)
{
foreach (var t in transfer.Inform)
{
...
}
}
...
}

I want to save 3 entities entry at a time. How to do that

I Have a view with 3 entity.
When I Click on the submit button, I Would like to save these entities .
In the view
<div class="editor-field ">
<%: Html.TextBoxFor(model => model.ElementAt(0).UserQuestion1)%>
<%: Html.ValidationMessageFor(model => model.ElementAt(0).UserQuestion1)%>
</div>
<div class="editor-field ">
<%: Html.TextBoxFor(model => model.ElementAt(0).UserAnswer)%>
<%: Html.ValidationMessageFor(model => model.ElementAt(0).UserAnswer)%>
</div>
<div class="editor-field ">
<%: Html.TextBoxFor(model => model.ElementAt(1).UserQuestion1)%>
<%: Html.ValidationMessageFor(model => model.ElementAt(1).UserQuestion1)%>
</div>
<div class="editor-field ">
<%: Html.TextBoxFor(model => model.ElementAt(1).UserAnswer)%>
<%: Html.ValidationMessageFor(model => model.ElementAt(1).UserAnswer)%>
</div>
<div class="editor-field ">
<%: Html.TextBoxFor(model => model.ElementAt(2).UserQuestion1)%>
<%: Html.ValidationMessageFor(model => model.ElementAt(2).UserQuestion1)%>
</div>
<div class="editor-field ">
<%: Html.TextBoxFor(model => model.ElementAt(2).UserAnswer)%>
<%: Html.ValidationMessageFor(model => model.ElementAt(2).UserAnswer)%>
</div>
In the Controller
public ActionResult ChooseQuestion()
{
List<UserQuestion> lst = new List<UserQuestion>() {
new UserQuestion(),new UserQuestion(), new UserQuestion()
};
return View(lst);
}
[HttpPost]
public void ChooseQuestion(List<UserQuestion> lst)
{
//lst is always NULL Why
//EntityFactory.GetEntity().SaveChanges();
}
Why when I Click on submit button, my parameter List lst is null.
I Would like to perform a save.
Thanks.
Rather than element at, try using the model[0] syntax instead. This is used in the examples I've seen.
HTH.
If you have a known number of entities you could construct a custom view model with a different name for each entity. They should be correctly bound when you submit them then.

asp.net mvc model item passed in to dictionary is of type List

I have a asp.net mvc view which is strongly typed view and i have a controller which returns
the ilist user based on the id provided. I am getting the following above error:
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[Data.User]', but this dictionary requires a model item of type 'Data.User'.
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Data.User>" %>
<% using (Html.BeginForm())
{%> <%= Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%= Html.LabelFor(model => model.Id) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.Id) %>
<%= Html.ValidationMessageFor(model => model.Id) %>
</div>
<div class="editor-label">
<%= Html.LabelFor(model => model.UserName) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.UserName) %>
<%= Html.ValidationMessageFor(model => model.UserName) %>
</div>
<div class="editor-label">
<%= Html.LabelFor(model => model.Email) %>
</div>
<div class="editor-field">
<%= Html.TextBoxFor(model => model.Email) %>
<%= Html.ValidationMessageFor(model => model.Email) %>
public ActionResult EditUser(int id)
{
var usr = _UsrService.GerUserById(id).ToList<User>();
return View(usr);
}
You can't pass a List to a modal that is to display a single item.
Try removing the .ToList<User> so that it looks something like
Data.User usr = _UsrService.GetUserById(id).FirstOrDefault;
return View(usr)
You've strongly typed your view class to a single instance of Data.User. Your action method is attempting to create the view by handing it a List<User>.
Since it looks like you just want to display a single user, try changing your action method to just get one user, like this:
public ActionResult EditUser(int id)
{
var usr = _UsrService.GerUserById(id).FirstOrDefault();
return View(usr);
}
I'm assuming that the GerUserById(id) method is returning an IEnumerable of some kind, since you're calling the ToList<> method on it. This means that even if it is a list of one item, you still need to get the object out of it, so calling FirstOrDefault() will give you the first result in the IEnumerable.
Assuming that there is only one user in your system with a given ID (which is sort of the definition of an ID), _UsrService.GerUserById(id) should be changed to return a User rather than a List<User>. Then your controller can just say:
return View(_UsrService.GetUserById(id));

Resources