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
Sachin R
NA
91
7.2k
Model binding returning default values for object from form
May 5 2015 2:23 PM
Model binding returning default values for the properties and simply refresh the form.. Its not posting the actual form data to server
Please help me on this to resolve
My code as below
View
@model TESTInternetMVC.ADONET_DAL.Read
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Read</legend>
@Html.HiddenFor(model => model.id)
<div class="editor-label" >
@Html.LabelFor(model => model.productname)
</div>
<div class="editor-field" >
@Html.EditorFor(model => model.productname)
@Html.ValidationMessageFor(model => model.productname)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.price)
</div>
<div class="editor-field" >
@Html.EditorFor(model => model.price)
@Html.ValidationMessageFor(model => model.price)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.description)
</div>
<div class="editor-field" >
@Html.EditorFor(model => model.description)
@Html.ValidationMessageFor(model => model.description)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
Model
public class Read
{
[Required]
public string productname;
[Required, DataType(DataType.Currency)]
public decimal price;
[Required]
public string description;
public int id;
Model also has this method for other operations
public IEnumerable<Read> read()
{
SqlConnection cn = DataAccess.sqlcn();
string q = "select id,productname,price,description from testinternetmvc..products";
//SqlCommand cmd = new SqlCommand(q, cn);
cn.Open();
SqlDataAdapter sda = new SqlDataAdapter(q, cn);
//DataSet ds = new DataSet();
DataTable dt=new DataTable();
//ds.Clear();
//sda.Fill(ds.Tables[0]);
sda.Fill(dt);
//ds = cmd.ExecuteReader();
//SqlDataReader d = cmd.ExecuteReader();
cn.Close();
IEnumerable<Read> dtlist = from r in dt.AsEnumerable()
select new Read { productname=r["productname"].ToString(),
price=Convert.ToDecimal(r["price"]),
description=r["description"].ToString(),
id=Convert.ToInt32(r["id"])
};
//List<Read> dtlist = dt.AsEnumerable().Select(row => new Read
//{
// productname = row.Field<string>("productname"),
// price = row.Field<decimal>("price"),
// description = row.Field<string>("description")
//}).ToList();
return dtlist;
}
Controller
[HttpGet]
public ActionResult Edit(int id)
{
Read rd = new Read();
Read data = rd.read().SingleOrDefault(d => d.id == id);
return View(data);
}
//
// POST: /ADONET/Edit/5
[HttpPost]
public ActionResult Edit(Read editeddata) //int id, FormCollection collection)
{
Response.Write(editeddata.id);
Response.Write(editeddata.description);
Response.Write(editeddata.price);
Response.Write(editeddata.productname);
try
{
if (ModelState.IsValid)
{
// TODO: Add update logic here
SPUpdate spu = new SPUpdate();
spu.Update(editeddata);
return RedirectToAction("Index");
}
else
return RedirectToAction("Index");
}
catch
{
return View();
}
}
Update method(Intended ADO.NET operations on posted data)
public class SPUpdate
{
//OPTION 1 - DIRECT UPDATE QUERY
public void Update(Read editeddata)
{
SqlConnection cn = DataAccess.sqlcn();
string q = "Update testinternetmvc..products set productname=@productname ,price=@price, description=@description where id=@oid";
SqlDataAdapter sda = new SqlDataAdapter(q,cn);
//sda.UpdateCommand.CommandText=q;
sda.UpdateCommand.Parameters.AddWithValue("@oid",editeddata.id);
sda.UpdateCommand.Parameters.AddWithValue("@productname",editeddata.productname);
sda.UpdateCommand.Parameters.AddWithValue("@price",editeddata.price);
sda.UpdateCommand.Parameters.AddWithValue("@description",editeddata.description);
//DataTable dt=new DataTable();
//sda.Fill(dt);
cn.Open();
sda.UpdateCommand.ExecuteNonQuery();
cn.Close();
}
}
Reply
Answers (
1
)
MVC
Authentication and authorization