Asp.net MVC 2 CascadingDropDown - asp.net-mvc-2

Hi, this is my Controller Code:
public ActionResult Create()
{
makelist = new SelectList(db.CountryMasters.ToList(), "CountryID", "CountryName",1);
ViewData["CounrtyMaster"] = makelist;
var modelList = new CascadingSelectList(clientMasterManagement.GetState(makelist.SelectedValue.ToString()), "StateID", "StateName");
ViewData["StateMaster"] = modelList;
return View("Create");
}
and this is my View Code:
<div class="editor-field">
<%= Html.DropDownList("CountryID", ViewData["CounrtyMaster"] as SelectList)%>
<%= Html.ValidationMessageFor(model => model.CountryID) %>
</div>
<%= Html.CascadingDropDownList("StateMaster","CountryMaster")%>
when I select upper list it's not affecting the second one, can anyone help me here???

User CascadingSelectList function instead of SelectList in your controller action
var makelist = new SelectList(db.CountryMasters.ToList(), "CountryID", "CountryName",1); ViewData["CounrtyMaster"] = makelist;
var modelList = new CascadingSelectList(clientMasterManagement.GetState(makelist.SelectedValue.ToString()), "StateID", "StateName");
ViewData["StateMaster"] = modelList;

Html.CascadingDropDownList is not a standard MVC helper. I take it that you are using Stephen Walther's helper. If so this question will help you out:
ASP.NET MVC - Cascading Drop Down

Related

Rails 4: scroll browser window to form after failed submission (validation errors)

So I have form (typical articles and comments example) for comments at bottom of page. If validation fails I display validation errors.
Thats my comments controller code:
class CommentsController < ApplicationController
before_action :authenticate_admin!, only: [:destroy]
expose(:article)
expose(:comment, attributes: :comment_params)
expose(:reply) { Reply.new }
def create
comment.article = article
if verify_recaptcha(model: comment, message: t('captcha_verification_error')) && comment.save
flash[:comment_notice] = t('comment_created_successfully')
redirect_to article_path(article) + '#comments'
else
flash[:comment_errors] = comment.errors.full_messages
render 'articles/show'
end
end
def destroy
comment.destroy
redirect_to article_path(article)
end
private
def comment_params
params.require(:comment).permit(:author, :content)
end
end
Here is form:
= simple_form_for(comment, url: article_comments_path(article)) do |f|
- if flash[:comment_errors]
.alert.alert-danger
strong= pluralize(flash[:comment_errors].count, 'error') + ' prohibited this article from being saved:'
- flash[:comment_errors].each do |msg|
ul
li= msg
fieldset class='form-group'
= f.label t('author')
= f.text_field :author, class: 'form-control', placeholder: t('who_are_you')
fieldset class='form-group'
= f.label t('content')
= f.text_area :content, class: 'form-control', rows: 6, placeholder: t('what_do_you_want_to_say')
fieldset class='form-group'
= recaptcha_tags
fieldset class='form-group'
= f.submit t('create_comment'), class: 'btn btn-primary'
For forms I'm using simple-form. Also I'm using decent exposure and slim
I want my page to scroll down to form after validation fails (So user don't have to scroll manually). Is there simple way to achieve that?
AFAIK I can't pass anchor to render (That would solve problem). Any ideas?
So I solved it with this javascript placed in comment form:
javascript:
if (document.getElementById("comment_errors")) {
location.hash = '#new_comment';
}
When element with id 'comment_errors' (My validation errors div) exists it jumps to it.

How to make checkbox checked based on the data from database in mvc 4 entity framework?

I am strong checkbox checked data in db. If the checkbox was checked i want to display it as checked in view. Is it possible to achieve this?
Here is my supporting code.
public ActionResult Index()
{
var groups = db.tm_grp_group.Where(a=>a.grp_isactive==true);
var permissions = db.tm_perm_level;
GroupPermissionVM model = new GroupPermissionVM
{
GroupList = new SelectList(groups, "grp_id", "grp_name"),
Permissions = permissions.Select(p => new PermissionVM
{
perm_id = p.perm_id,
perm_levelname = p.perm_levelname
})
};
return View(model);
}
This is index.cshtml
#model Permission.Models.GroupPermissionVM
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
#using (Html.BeginForm())
{
#Html.LabelFor(m=>m.GroupID)
#Html.DropDownListFor(m => m.GroupID, Model.GroupList, "Please select", new { id = "ddlgrp" })
#Html.EditorFor(m => m.Permissions)
<p><input type="submit" value="Submit" /></p>
}
Inside Home/Editortemplate/PermissionVM.cshtml
#model Permission.Models.PermissionVM
#{
ViewBag.Title = "PermissionVM";
}
<div>
#Html.HiddenFor(m => m.perm_id)
#Html.HiddenFor(m => m.perm_description)
#Html.CheckBoxFor(m => m.perm_status)
#Html.LabelFor(m => m.perm_status,Model.perm_levelname)
</div>
I have table structure like this
grp_id perm_id
1 1 1
2 2 2
permlevel_name perm_status
1 Screen Level True
2 custom Level True
Grp_id is dropdownlistbox in this context. So if i select 1 corresponding permlevel_name is screen level and it is checked. So when i make selection as 1 below screenlevel checkbox should be cheked. Is it possible to achieve this?
in your code where you call PermissionVM.cshtml File.

Create the tx_news searchform in the layout

I would like to create the tx_news searchform on every page. So I try it to put it into the Layout:
<div class="news-search-form">
<f:form object="{search}" name="search" pageUid="{settings.searchPid}">
<fieldset>
<f:form.textfield id="news-subject" property="subject" class="form-control"/>
<f:form.submit class="btn btn-default search-button"/>
</fieldset>
</f:form>
</div>
The problem is, that {search} is not defined when there is no searchplugin on the site.
When there is a searchplugin {search} looks like this:
Tx_News_Domain_Model_Dto_Search (prototype transient entity)
subject => NULL
fields => NULL
uid => NULL
_localizedUid => NULL
_languageUid => NULL
pid => NULL
Am I somehow able to create this directly in the layout or what would be the best way to have the searchform on every page?
You can use direct TS easy
lib.blogsearch = USER
lib.blogsearch{
userFunc = tx_extbase_core_bootstrap->run
extensionName = News
pluginName = Pi1
switchableControllerActions {
News {
1 = searchform
}
}
settings < plugin.tx_news.settings
settings {
listPid = #you_id
}
}
Have you tried to insert the searchform via typoscript as records?
To do this, you should create the news plugin configured to search form somewhere in your pagetree, and reference it via ts like this:
lib.searchform = RECORDS
lib.searchform {
wrap = (whatever)|
tables = tt_content
source = {id of the searchform CE}
}
This should do the trick.

How to wrap radio button group in label zend form

I want the following html with two radio button groups in zend form
<label class="inline">
<input type="radio" name="form-field-radio">
<span class="lbl"> Male</span>
</label>
<label class="inline">
<input type="radio" name="form-field-radio">
<span class="lbl"> Female</span>
</label>
and i'm using the following code to make this button in Zend Form
$gender = $this->CreateElement('radio','gender')
->addFilter(new Zend_Filter_StringTrim())
->setMultiOptions(array('M'=>'Male', 'F'=>'Female'))
->setDecorators(array( array('ViewHelper') ));
But I don't know where to set the lable and span classes in this code.
Please help.
Thanks.
I am not sure whether its the perfect method to do this its the first time i am using zend framework, But still here are the steps that i did, if you may find useful:
First I created a custom decorator which extends Zend_Form_Decorator_Abstract and saved it in the location 'decorator/My_Form_Decorator.php'. decorator is a directory created by me in root.
.
/decorator
/application
etc...
Then i included it in a controller. I have read that there are certain methods for adding the decorator like addPrefixPath() but for time sake i just included the decorator file in top with 'include "../decorator/My_Form_Decorator.php";'. Then instead of using the CreateElement method i used Zend_Form_Element.
The following is the code of custom radio decorator
My_Form_Decorator.php
class My_Decorator_RadioInput extends Zend_Form_Decorator_Abstract
{
public function render($content)
{
$element = $this->getElement();
$label = htmlentities($element->getLabel());
$type = $element->type;
$name = $element->elemName;
$multiOptions = $element->multiOptions;
$labelClass = $element->labelClass;
$spanClass = $element->spanClass;
$markup='';
if(!empty($type) && !empty($name) && !empty($multiOptions) && is_array($multiOptions)){
foreach($multiOptions as $key=>$value){
$markup .='<label class="'.$labelClass.'"><input type="radio" name="'.$name.'" value="'.$key.'"> <span class="'.$spanClass.'">'.$value.'</span></label>';
}
}
return $markup;
}
}
and this is the code in my controller function
IndexController.php
$decorator = new My_Decorator_RadioInput();
$form = new Zend_Form();
$form->setAttrib('id', 'test');
$element = new Zend_Form_Element('foo', array(
'elemName'=>'gender',
'type' =>'radio',
'multiOptions' => array('M'=>'Male', 'F'=>'Female'),
'labelClass'=>'inline',
'spanClass'=>'lbl',
'decorators' => array($decorator),
));
$form->addElement($element);
$this->view->form = $form;
And in view index.phtml
echo $this->form
Hope this is helpful to you..

How to preselect item in Html.DropDownlListFor()

How can i preselect item in Html.DromDownListFor() ?
i have code in view which inserts items to DropDownListFor
<div class="editor-field">
<%var mesta = new List<SelectListItem>();
SelectListItem aa = new SelectListItem();
aa.Text = "---------VYBER MESTO---------";
aa.Value = "0";
mesta.Add(aa);
foreach (var item in Model.MestoTbl)
{
SelectListItem a = new SelectListItem();
a.Text = item.Mesto;
a.Value = item.MestoId.ToString();
mesta.Add(a);}%>
<%: Html.DropDownListFor(model => model.Mesto.MestoId, mesta)%>
<%: Html.ValidationMessageFor(model => model.Mesto.MestoId)%>
</div>
this inserts 2 values MestoId & Mesto ....when i click on some database record (edit field)
example =>
Name Surname Mesto
--------------------
Peter Malik Snina
Snina => Mestoid = 2
I wanna get ....
if i click to edit record of Peter Malik the Html.DropDownListFor automatically preselect item Snina in list.
You could use the SelectList constructor. See here.
thanks now it is working.
<%: Html.DropDownListFor(model => model.Mesto.MestoId, new SelectList(mesta, "Value", "Text", Model.Ziak.MestoId))%>