TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Guest User
Tech Writer
189
24.8k
i cant solve this error please help me
Aug 1 2018 1:52 AM
//ERROR
There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'ProductID'.
[HttpGet
public
bool
SelectProduct(clproduct clproduct)
{
using
(MySqlConnection con =
new
MySqlConnection())
{
con.ConnectionString = constr;
using
(MySqlCommand cmd =
new
MySqlCommand())
{
cmd.Connection = con;
cmd.CommandText =
"insert into clientproduct (ProductId,ClientProductDetail,Act_Date,Exp_Date,CLProductId,ParentProductId) values ('"
+ clproduct.ProductId +
"','"
+ clproduct.ClientProductDetail +
"',"
+
"'"
+ clproduct.Act_Date +
"','"
+ clproduct.Exp_Date +
"','"
+ clproduct.CLProductId +
"','"
+ clproduct.ParentProductId +
"')"
;
con.Open();
i = cmd.ExecuteNonQuery();
con.Close();
return
true
;
}
return
false
;
}
}
]
public
ActionResult SelectedProduct()
{
MySqlConnection con =
new
MySqlConnection(constr);
MySqlDataAdapter da =
new
MySqlDataAdapter(
"SELECT ProductId,ProductName,Description FROM ProductMaster"
, con);
DataTable dt =
new
DataTable();
da.Fill(dt);
ViewBag.ProductNM = ToSelectList(dt,
"ProductID"
,
"ProductName"
);
ViewBag.productid = ToSelectList(dt,
"ProductId"
,
"ProductId"
);
ViewBag.desc = ToSelectList(dt,
"ProductId"
,
"Description"
);
return
View();
}
[HttpPost]
public
ActionResult SelectedProduct( clproduct cl)
{
string
msg;
if
(ModelState.IsValid)
{
ModelState.Clear();
try
{
if
(i > 1)
{
SelectProduct(cl);
Response.Write(
"<script> alert('Records added Successfully.')<script>"
);
return
RedirectToAction(
"Index"
);
}
else
{
msg =
"All Fields Required"
;
}
}
catch
{
Response.Write(
"<script> alert('Records not added..')<script>"
);
return
View(
"Index"
);
}
}
else
{
return
View(
"invalid"
);
}
ViewBag.Message = msg;
return
View();
}
[NonAction]
public
SelectList ToSelectList(DataTable dtable,
string
viewfield,
string
textfield)
{
List<SelectListItem> list =
new
List<SelectListItem>();
foreach
(DataRow drow
in
dtable.Rows)
{
list.Add(
new
SelectListItem()
{
Text =drow[textfield].ToString(),
Value=drow[viewfield].ToString()
});
}
return
new
SelectList(list,
"Value"
,
"Text"
);
}
//MODEL
public
class
clproduct
{
[Display(Name =
"ClpID"
)]
public
string
CLProductId {
get
;
set
; }
[Display(Name =
"Prod ID"
)]
public
string
ProductId {
get
;
set
; }
[Display(Name =
"P_ProductID"
)]
public
string
ParentProductId {
get
;
set
; }
public
string
Act_Date {
get
;
set
; }
public
string
Exp_Date {
get
;
set
; }
[Display(Name =
"Description"
)]
public
string
ClientProductDetail {
get
;
set
; }
}
View
@model Clients.Models.clproduct
@{
Layout =
null
;
}
<!DOCTYPE html>
<html>
<head>
<meta name=
"viewport"
content=
"width=device-width"
/>
<title>Select Product</title>
</head>
<body>
<div>
@using (Html.BeginForm(
"SelectedProduct"
,
"ClientMaster"
, FormMethod.Post,
new
{ id =
"popupForm"
}))
{
if
(ViewBag.Message !=
null
)
{
<div
class
=
"alert alert-danger"
>
@ViewBag.Message
</div>
}
<div
class
=
"mycontainer"
>
<div
class
=
"row"
>
<div
class
=
"col-lg-10"
>
<table
class
=
"table table-responsive"
>
<tr>
<td colspan=
"3"
>
<div
class
=
"col-md-6"
><label>ClPID</label></div>
<div
class
=
"col-md-8"
>@Html.TextBoxFor(model => model.CLProductId)</div>
</td>
</tr>
<tr>
<td>
<div
class
=
"col-md-6"
><label>ProductID</label></div>
<div
class
=
"col-md-8"
>
@*<select id=
"ProductID"
class
=
"pc form-control"
>
<option>@ViewData[
"ProdID"
]</option>
</select>*@
@Html.DropDownList(
"ProductID"
, (IEnumerable<SelectListItem>)ViewBag.ProductNM,
"Select Product"
,
new
{ @
class
=
" form-control"
, @style =
"width:200px"
})
</div>
</td>
<td>
<div
class
=
"col-md-6"
><label>Product</label></div>
<div
class
=
"col-md-8"
>@Html.TextBoxFor(model => model.ClientProductDetail)</div>
</td>
<td>
<div
class
=
"col-md-6"
><label>Description</label></div>
<div
class
=
"col-md-8"
><input type=
"text"
id=
"txt_Description"
/></div>
</td>
</tr>
<tr>
<td>
<div
class
=
"col-md-6"
><label>Act_Date</label></div>
<div
class
=
"col-md-8"
>@Html.TextBoxFor(model => model.Act_Date,
new
{ @id =
"ADate"
})</div>
</td>
<td>
<div
class
=
"col-md-6"
><label>Exp_Date</label></div>
<div
class
=
"col-md-8"
>@Html.TextBoxFor(model => model.Exp_Date,
new
{ @id =
"EDate"
})</div>
</td>
<td>
<div
class
=
"col-md-6"
><label>Parent ProdID</label></div>
<div
class
=
"col-md-8"
>@Html.TextBoxFor(model => model.ParentProductId)</div>
</td>
</tr>
</table>
</div>
</div>
<div
class
=
"col-lg-10"
>
<table
class
=
"table table-responsive"
>
<tr>
<td>
<div
class
=
"col-md-8"
>
<input type=
"submit"
class
=
"btn btn-success"
/>
</div>
</td>
<td>
<div
class
=
"col-md-8"
>
<input type=
"reset"
class
=
"btn btn-default"
/>
</div>
</td>
</tr>
</table>
</div>
</div>
}
</div>
</body>
</html>
@*<script src=
"http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.10.0.min.js"
type=
"text/javascript"
></script>
<script src=
"http://ajax.aspnetcdn.com/ajax/jquery.ui/1.9.2/jquery-ui.min.js"
type=
"text/javascript"
></script>*@
@* *@
<script type=
"text/javascript"
>
$(
function
() {
$(
'#ADate'
).datepicker({
dateFormat:
'mm-dd-yy'
})
$(
'#EDate'
).datepicker({
dateFormat:
'mm-dd-yy'
})
})
</script>
Reply
Answers (
8
)
how to fix timeout expired in C#.net and MS sql server
Server Issue....