I'm pretty new to MVC.
I'm trying to develop an MVC webpage. I'm trying to use #HtmlDropDownList(), which would have to populate values from my web.config, using a ViewBag. The page is getting rendered with the correct values, however on the output screen, there is no output. The dropdown shows only empty values on screen. Can someone please help!
Webconfig code:
<add key="ddlStreams" value="Las Vegas|India|Australia"/>
Controller code:
public ActionResult Index()
{
string reportTypes = ConfigurationManager.AppSettings["ddlStreams"].ToString();
ViewBag.ddlStreamsVB = reportTypes.Split('|')
.Select((value) => new SelectListItem { Value = value.ToString() });
GetData getdata = new GetData();
return View(getdata.GetDataFromTable());
}
This is my view:
<div >
<div class="form-control">
<div class="row">
<div class="col-sm-4">
#Html.Label("Stream name")
</div>
<div class="col-md-8">
#Html.DropDownList("ddlStreamsVB1", ViewBag.ddlStreamsVB as IEnumerable<SelectListItem>)
</div>
</div>
</div></div>
The rendered HTML on clicking on View source:
<div >
<div class="form-control">
<div class="row">
<div class="col-sm-4">
<label for="Stream_name">Stream name</label>
</div>
<div class="col-md-8">
<select id="ddlStreamsVB1" name="ddlStreamsVB1"><option value="Las Vegas"></option>
<option value="India"></option>
<option value="Australia"></option>
</select>
</div>
</div>
</div>
</div>
The final output :(
Final output here!!
Issue is you are not setting Text prop of SelectListItem, update as below:
ViewBag.ddlStreamsVB = reportTypes.Split('|')
.Select((value) => new SelectListItem
{ Value= value.ToString(),
Text = value
});
Related
I want to display data from 2 different tables which are stored in ViewBag at view side but I'm unable to print the data, I get an error
'object' does not contain a definition for 'Name'
The code at controller side
public ActionResult Packages()
{
int Prov_id = 1;
using (var db = new DataContex())
{
ViewBag.pack = db.Packages.Where(x => x.ProviderId == Prov_id).ToList();
ViewBag.service = db.GroupServices.Join(db.PackageServices,
gs => gs.ServiceId,
ps => ps.ServiceId,
(gs, ps) => new
{
name = gs.ServiceName
})
.ToList();
return View();
}
View markup:
#foreach (var item in ViewBag.pack)
{
<div class="col-md-6 bg-white mt-3">
<div class="card border package-grid p-3">
<div class="row">
<div class="col-md-8">
<input type="hidden" />
<h4 class="font-weight-bold py-2">#item.PackageName</h4>
#foreach (var item1 in ViewBag.service)
{
<h6 class="py-1">#item1.Name</h6>
}
<p class="py-3">#item.PackageDescription</p>
</div>
</div>
<div class="row">
<div class="col-md-12 mt-3 d-flex justify-content-between flex-wrap">
<p><strike> Rs. #item.PackagePrice</strike> <span>Rs. #item.PackageOfferPrice</span> </p>
<span>Remove</span>
</div>
</div>
</div>
</div>
}
That is because when you created ViewBag.service you have created anonymous object with property as name with n as LowerCase and at view side you are trying to use #item.Name as N as UpperCase.
Correct the property name and it will work.
I am following an example in primeng in which I can add a new row to a table. Everything "seems" to work as long as I fill out all the fields in the input option. However, I want to add the place holder value to the NGmodel if the user does not change the value of the input. I tried everything (ng-init, ngvalue, etc etc) but I can never get the ngmodel to carry the value in the place holder... and the table gets fill with the 3 filled fields but not the one where the user did not type anything.
extract of the HTML....
<div class="ui-g-12">
<div class="ui-g-4">
<label for="product_line_id">Product ID</label>
</div>
<div class="ui-g-8">
<input pInputText id="product_line_id" [ngModel]="myproduct.product_line_id" placeholder="{{ lastproductline + 1}}" />
</div>
</div>
<div class="ui-g-12">
<div class="ui-g-4">
<label for="product_line_1">Product</label>
</div>
<div class="ui-g-8">
<input pInputText id="product_line_1" [(ngModel)]="myproduct.product_line_1" autofocus />
</div>
</div>
<div class="ui-g-12">
<div class="ui-g-4">
<label for="product_line_2">Category</label>
</div>
<div class="ui-g-8">
<input pInputText id="product_line_2" [(ngModel)]="myproduct.product_line_2" />
</div>
</div>
<div class="ui-g-12">
<div class="ui-g-4">
<label for="product_line_3">Sub Category</label>
</div>
<div class="ui-g-8">
<input pInputText id="product_line_3" [(ngModel)]="myproduct.product_line_3" />
</div>
</div>
the ts file looks something like...
productlines = [];
myproduct: { [s: string]: ProductLines; } = {};
showDialogToAdd() {
this.newProductLine = true;
this.myproduct = {};
this.displayDialog = true;
}
save() {
let productlines = [...this.productlines];
productlines.push(this.myproduct);
this.finalproductchanges.push(this.myproduct)
this.productlinesClone = productlines;
this.myproduct = null;
this.displayDialog = false;
}
Any ideas will be greatly appreciated
I managed to change the code by adding the field directly in the controller.
I am trying to get a value from a textbox of my View.
This is my View:
#model MyDataIndexViewModel
#{
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<h1>Meine Daten</h1>
</div>
</div>
var item = Model.User;
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6 myDataTitle">Email</div>
<div class="col-xs-6 col-sm-6 col-md-6">
#Html.TextBox("txtEmail", "", new { placeholder = item.Email})
</div>
</div>
}
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<a class="btn btn-default pull-right" href="/ChangeMyData/Save">Speichern</a>
</div>
</div>
This is my Controller:
[HttpPost]
public ActionResult Save()
{
var email = Request["txtEmail"].ToString();
return View();
}
I get the error just as it says in the Title.
Thank you in advance!
VIEW:
#model MyDataIndexViewModel
#using (Html.BeginForm("Save", "CONTROLLER_NAME"))
{
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<h1>Meine Daten</h1>
</div>
</div>
var item = Model.User;
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6 myDataTitle">Email</div>
<div class="col-xs-6 col-sm-6 col-md-6">
#Html.TextBox("txtEmail", "", new { placeholder = item.Email, id="txtEmail"})
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<a class="submit btn btn-default pull-right">Speichern</a>
</div>
</div>
}
CONTROLLER
[HttpPost]
public ActionResult Save()
{
var email = Request.Form["txtEmail"].ToString();
return View();
}
You can either use strongly-typed viewmodel binding:
View
#model MyDataIndexViewModel
#* other stuff *#
#Html.TextBox("txtEmail", Model.Email)
Controller
[HttpPost]
public ActionResult Save(MyDataIndexViewModel model)
{
var email = model.Email;
return View();
}
Or use a TextBoxFor directly for model binding:
#model MyDataIndexViewModel
#* other stuff *#
#Html.TextBoxFor(model => model.Email)
Or if you still want to use HttpRequest members, Request.Form collection (a NameValueCollection) is available to retrieve text from txtEmail input:
[HttpPost]
public ActionResult Save()
{
var email = Request.Form["txtEmail"].ToString();
return View();
}
Note that Request["txtEmail"] is discouraged due to no compile time safety applied for it, because the key value may retrieved from Request.QueryString, Request.Form or other HttpRequestBase members.
Similar issue:
MVC TextBox with name specified not binding model on post
Access form data into controller using Request in ASP.NET MVC
I have a partial view _ABC.cshtml that is hidden initially.
<div id="overview" style="display:none">
<h1> Overview</h1>
<div class="col-xs-3 col-sm-3 col-md-3 col-lg-3 leftlist">
</div>
<div class="col-xs-9 col-sm-9 col-md-9 col-lg-9 overview">
<h3 class="isppagetitle">#Resources.Overview</h3>
<div class="row col-xs-12 col-sm-12 col-md-12 col-lg-12 content">
<div class="row">
......
But now from other view I want it to be rendered .
public ActionResult Index()
{
return PartialView("_AMgmtPartial");
}
I did in _AMgmtPartial..
<div class="account-root">
#{
Html.RenderPartial("_CustList");
Html.RenderPartial("_ABC", new { style = "display:block" });
}
<input type="hidden" id="accountmgmturl" value='#Url.Action("", "AccountMgmt")' />
...............
...............
But this style="display:block;" did not work. What did I do wrong ?
Html.RenderPartial doesn't have a parameter that accepts HtmlAttributes. This is confirmed by looking at the documentation for it.
The parameter that you have populated would be accessible from within the Controller though:
public ActionResult Index(string style)
{
// style will equal "display:block"
return PartialView("_AMgmtPartial");
}
You could take this value and pass it to your partial view via the view bag or a view model.
public ActionResult Index(string style = "display: none")
{
// style will equal "display:block"
ViewBag.PartialStyle = style;
return PartialView("_AMgmtPartial");
}
Then this makes it accessible from within your view:
<div id="overview" style="#ViewBag.PartialStyle">
<h1> Overview</h1>
<div class="col-xs-3 col-sm-3 col-md-3 col-lg-3 leftlist">
</div>
<div class="col-xs-9 col-sm-9 col-md-9 col-lg-9 overview">
<h3 class="isppagetitle">#Resources.Overview</h3>
<div class="row col-xs-12 col-sm-12 col-md-12 col-lg-12 content">
<div class="row">
......
To do this though would be a dirty hack. As #Stephen mentioned a more preferable solution would be to wrap your partial view render statement in a <div> and on that you should set your initial visibility.
<div style="display: block">
#Html.Partial("_ABC");
</div>
I have scenario where I have load data on dropdown selection change and after manipulation I have to submit the page for saving.
I have developed the page but since I new to MVC so can anyone verify me code and can suggest better approach of doing the same.
#using (Html.BeginForm("Save", "forecast"))
{
using (Html.BeginForm("YearChange", "forecast", FormMethod.Get, new {#id="frmYearChange" }))
{
<div class="span12">
<div class="well well-sm" style="padding-bottom: 0px !important;">
<div class="span5">
<div class="control-group">
<label class="control-label" for="selectGroup">
Select Group:</label>
<div class="controls">
#Html.DropDownListFor(m => m.groupId, Model.groupList, "--Select Group--", new { #id = "selectGroup" })
</div>
</div>
</div>
<div class="span5">
<div class="control-group">
<label class="control-label" for="selectYear">
Select Year:</label>
<div class="controls">
#Html.DropDownListFor(m => m.yearId, Model.yearList, "--Select Year--", new { #id = "selectYear", onchange = "selectYear_indexChanged();" })
</div>
</div>
</div>
<div class="clearfix">
</div>
</div>
</div>
}
.....
}
You can't have nested forms. You can however have multiple forms per view.
See W3C for more info.