I have two controller methods one contains a WebGrid with Edit and Delete functionality. On Edit I have a Partial View opening in model popup. In Edit View HtmlElements are generated dynamically on the basis of IDictionary object. Here is my Controller code:
using MVCWebGridDemo.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.Linq;
using System.Web;
using System.Web.Helpers;
using System.Web.Mvc;
namespace MVCWebGridDemo.Controllers
{
public class CtiServicesController : Controller
{
CRMDBContext dbContext = new CRMDBContext();
public ActionResult Index()
{
var dtSerivices = dbContext.FetchServiceList();
var model1 = new List<dynamic>(dtSerivices.Rows.Count);
List<WebGridColumn> columns = new List<WebGridColumn>();
foreach (DataRow row in dtSerivices.Rows)
{
var obj = (IDictionary<string, object>)new ExpandoObject();
foreach (DataColumn col in dtSerivices.Columns)
{
// string.Format("{0}", "format: @< text > < span class=\"display-mode\">{1}</span> <label id = {2} class=\"edit-mode\">{3}</label> </text>, style: \"col1Width\" ",col.ColumnName,row[col], col.ColumnName, row[col]);
obj.Add(col.ColumnName, row[col]);
if (columns.Count < dtSerivices.Columns.Count)
{
columns.Add(new WebGridColumn() { ColumnName = col.ColumnName, Header = col.ColumnName.Replace("_", " ") });
}
}
model1.Add(obj);
}
columns.Add(new WebGridColumn()
{
Header = "Edit",
Format = (item) =>
{
return new HtmlString(string.Format("<a href =\"javascript:void(0);\" class=\"edit-row\" id=\"{0}\">Edit</a>", item.id));
}
});
columns.Add(new WebGridColumn()
{
Header = "Delete",
Format = (item) =>
{
return new HtmlString(string.Format("<a href =\"javascript:void(0);\" class=\"delete-row\" id=\"{0}\">Delete</a>", item.id));
}
});
ViewBag.Columns = columns;
return View(model1);
}
public ActionResult EditGrid(int id)
{
var obj = (IDictionary<string, object>)new ExpandoObject();
try
{
var dtSerivices = dbContext.FetchServiceList();
var dataModel = dtSerivices.AsEnumerable().Where(x => x.Field<int>(0) == id).FirstOrDefault();
foreach (DataColumn dc in dtSerivices.Columns)
{
obj.Add(dc.ColumnName, dataModel[dc.ColumnName]);
}
}
catch (Exception ex)
{
string exception = ex.Message;
}
//ViewData["Member"] = obj;
return PartialView("EditGrid", obj);
}
[HttpPost]
public ActionResult Update(IDictionary<string,object> mobj)
{
foreach(KeyValuePair<string,object> entry in mobj)
{
var result = entry.Key;
}
return new EmptyResult();
}
public ActionResult DeleteGrid(int id)
{
MyClass myClass = new MyClass();
myClass.id = id.ToString();
return View();
}
}
}
On Save button click I want to save the updated values, but on Update Action method i am unable to see those values instead I am getting controller and action, see screenshot
Update Action Method Screenshot
If the key or value type is a complex type (like Company, in the above example), then we treat parameterName[index].Key or parameterName[index].Value as the entire field prefix and start appending the .PropertyName suffix to the
end of it. The index must also be zero-based and unbroken .
Please tell me where I am doing wrong and why I am not getting values in object properties here. It will be much helpful for me if you can show me a sample code.
[HttpPost]
public ActionResult Update(IDictionary<string,object> mobj)
{
foreach(KeyValuePair<string,object> entry in mobj)
{
var result = entry.Key;
}
return new EmptyResult();
}
Member
30 Points
184 Posts
Save dynamic data to DB on Submit Click
Sep 22, 2018 02:10 PM|shashikant2011|LINK
Hi,
I have two controller methods one contains a WebGrid with Edit and Delete functionality. On Edit I have a Partial View opening in model popup. In Edit View HtmlElements are generated dynamically on the basis of IDictionary object. Here is my Controller code:
Index View:
Here is my Partial View for Edit which opens in Model Popup on click of Edit in above WebGrid
On Save button click I want to save the updated values, but on Update Action method i am unable to see those values instead I am getting controller and action, see screenshot Update Action Method Screenshot
please help
All-Star
18815 Points
3831 Posts
Re: Save dynamic data to DB on Submit Click
Sep 24, 2018 03:16 AM|Nan Yu|LINK
Hi Sanjeev,
Please look at article : https://www.hanselman.com/blog/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.aspx
If the signature looks like this:
You will create html like :
If the key or value type is a complex type (like Company, in the above example), then we treat parameterName[index].Key or parameterName[index].Value as the entire field prefix and start appending the .PropertyName suffix to the end of it. The index must also be zero-based and unbroken .
You can also refer to this thread :
https://stackoverflow.com/questions/5191303/asp-net-mvc-binding-to-a-dictionary
Best Regards,
Nan Yu
Member
30 Points
184 Posts
Re: Save dynamic data to DB on Submit Click
Sep 24, 2018 08:08 AM|shashikant2011|LINK
Thanks for your reply.
Please tell me where I am doing wrong and why I am not getting values in object properties here. It will be much helpful for me if you can show me a sample code.
Thanks
All-Star
18815 Points
3831 Posts
Re: Save dynamic data to DB on Submit Click
Sep 25, 2018 06:38 AM|Nan Yu|LINK
Hi Sanjeev,
We read in the properties is by looking for parameterName[index].PropertyName. The index must be zero-based and unbroken. So modify your code like :
Then you could find the data in controller :
Best Regards,
Nan Yu
Member
30 Points
184 Posts
Re: Save dynamic data to DB on Submit Click
Sep 27, 2018 11:04 AM|shashikant2011|LINK
Thank you so much Nan Yu, for your sample code which helped me a lot. Finally my problem is resolved.