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))%>
Related
I'm using the framework Phalcon. I'm trying to create a form to get a value (an ID called "idcliente") from a table (in mysql) called cliente, which has 2 columns: "idcliente" and "nombre". With this value I want to update a field (also "idcliente") on another table called Users.
My form is this:
class AsignarclienteForm extends Form{
public function initialize($entity = null, $options = null){
$idcliente = new Select('idcliente',[
Cliente::find(),
"useEmpty" => true,
"emptyText" => "Please select...",
"using" => ["idcliente", "nombre"],
]);
$idcliente->setLabel('ID Cliente');
$idcliente->addValidators(array(
new PresenceOf(array(
'message' => 'idcliente is required'
))
));
$this->add($idcliente);
}
}
And my controller:
public function asignarclienteAction(){
$auth = $this->session->get('auth');
$permiso = $auth['active'];
$id = $auth['id'];
if($permiso!='A'){return $this->forward('servicios/index');}
$form = new AsignarclienteForm;
if ($this->request->isPost()) {
$idcliente = $this->request->getPost('idcliente');
$sql = "UPDATE Users SET idcliente = ?0 WHERE id = ?1";
$this->modelsManager->executeQuery($sql, array(0 => $idcliente, 1 => $id));
return $this->forward('admin/usuarios');
}
$this->view->form = $form;
}
And my view:
<div class="panel-body">
{{ form('admin/asignarcliente/', 'id': 'asignarclienteForm', 'onbeforesubmit': 'return false') }}
<fieldset>
<div class="control-group">
{{ form.label('idcliente', ['class': 'control-label']) }}
<div class="controls">
{{ form.render('idcliente', ['class': 'form-control']) }}
</div>
</div>
<div class="form-actions">
{{ submit_button('Asignar', 'class': 'btn btn-primary', 'onclick': 'return SignUp.validate();') }}
</div>
</fieldset>
</form>
</div>
I got the following error in the web site:
ID Cliente
Catchable fatal error: Object of class Phalcon\Mvc\Model\Resultset\Simple could not be converted to string in C:\xampp\htdocs\OpinionZoom\cache\volt\c%%%%xampp%%htdocs%%opinionzoom%%app%%views%%admin%%asignarcliente.volt.php on line 31
Where line 31 is {{ form.render('idcliente', ['class': 'form-control']) }}
on my view
I haven't found enough documentation of how to create a form with select, despite I have created a lot of forms.
If someone could help me I would appreciate it a lot. Thanks.
Your element definition in your form Asignarclienteform is incorrect.
The first parameter of Select must be a string (the name of your element).
The second parameter takes the options of your select element.
// Select construct parameters
Select(string $name, [object | array $options], [array $attributes])
I moved idcliente out of the array into the first parameter position:
$idcliente = new Select('idcliente', Cliente::find(), [
"useEmpty" => true,
"emptyText" => "Please select...",
"using" => ["idcliente", "nombre"],
]);
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.
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.
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
I've a problem with DropDownlist Name in ASP.NET MVC
In my EditorTemplate, I've
<%: Html.DropDownList("PoolGeometry",Model.selectVm.PoolGeometry, new { id = "poolgeometry" })%>
In generate html, I've
<select name="Pool.PoolGeometry" id="poolgeometry">
Normally, "PoolGeometry" is a field in db. If my dropdownlistname has the same name, selected value is value of field.
I don't understand this automatic rename!
EDIT :
Name is dependent on EditorTemplate :
if EditorTemplate called like this:
<%: Html.EditorFor(model => model.Pool,"SwimmingPool","")%>
Name of dropdownlist is "PoolGeometry" and selectedvalues are ok.
But if it is called like this:
<%: Html.EditorFor(model => model.Pool,"SwimmingPool")%>
Name of dropdownlist is "Pool.PoolGeometry"