Linq Join Two Repository Asp.Net MVC - asp.net-mvc

I have two Repository Stock and Inventory ,Both have Get , Insert ,Update ,delete Methods
private IStockService _stockService;
private IInventoryService _inventoryService;
public StockController(IStockService StockService, IInventoryService InventoryService)
{
_stockService = StockService;
_inventoryService = InventoryService;
}
InterFace is
public interface IStockService
{
IEnumerable<Stock> GetStock();
/// <summary>
///
/// </summary>
/// <returns></returns>
IEnumerable<Stock> GetStock(Func<Stock, bool> where);
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
Stock GetStockById(int id);
/// <summary>
///
/// </summary>
/// <param name="objStock"></param>
void InsertStock(Stock objStock);
/// <summary>
///
/// </summary>
/// <param name="objStock"></param>
void UpdateStock(Stock objStock);
/// <summary>
///
/// </summary>
/// <param name="objStock"></param>
void DeleteStock(Stock objStock);
}
I Want To Join Two Repository Using Linq Query eg: from stock in _stock JOIN etc

You should join on GetStock() method of the first repository and ... method of the second repository with their equals fields.
Var result1 = repo.getstock();
Var result2 = repo2.yourMethodName();
And join with linq on result1 and result2

Related

Register all modules exported using MEF and autofac

Using the Autofac.Mef extension I want to register exported Modules. Is there a way?
Exporting
[Export(typeof(IModule))]
public class MyModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<SampleJob>().AsSelf();
}
}
Registering
var catalog = new DirectoryCatalog(".");
builder.RegisterComposablePartCatalog(catalog); //This would register all exported types.
builder.RegisterModule(//All IModules registerd as that type I want to register as Modules)
There is a very good writeup about one such approach in Unleashing modules – part 2. The approach is to use a delegate as a factory to create the modules instead of exporting the IModule type.
using Autofac;
/// <summary>
/// Creates an <see cref="IModule" /> that's responsible for loading
/// dependencies in the specified context.
/// </summary>
/// <param name="mode">The mode the application is running in, let's the implementor
/// of the factory load different dependenices for different modes.</param>
/// <returns>An <see cref="IModule" />.</returns>
public delegate IModule ModuleFactory(ApplicationMode mode);
/// <summary>
/// Describes different modes an application can run in.
/// </summary>
public enum ApplicationMode
{
/// <summary>
/// The application is running in a test environment.
/// </summary>
Test = 0,
/// <summary>
/// The application is running in a staging environment.
/// </summary>
Staging = 1,
/// <summary>
/// The application is running in production environment.
/// </summary>
Production = 2
}
The real magic happens in the ComposedModule.
using Autofac.Builder;
using Autofac;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition.Primitives;
using System.Collections.Generic;
using System;
/// <summary>
/// An <see cref="Autofac.Builder.Module" /> is composed by with the help of
/// the System.ComponentModel.Composition (MEF) framework.
/// </summary>
public class ComposedModule
: global::Autofac.Builder.Module
{
#region Fields
private ApplicationMode mode;
private ComposablePartCatalog catalog;
[Import(typeof(ModuleFactory))]
private IEnumerable<ModuleFactory> RegisteredModuleFactories;
#endregion
#region Construction
/// <summary>
/// Creates a new ComposedModule using the specified catalog to
/// import ModuleFactory-instances.
/// </summary>
/// <param name="mode">The mode the application is running in.</param>
/// <param name="catalog">The catalog used to import ModuleFactory-instances.</param>
public ComposedModule(ApplicationMode mode, ComposablePartCatalog catalog)
{
if (catalog == null) throw new ArgumentNullException("catalog");
this.mode = mode;
this.catalog = catalog;
this.ImportFactories();
}
/// <summary>
/// Creates a new ComposedModule that loads all the ModuleFactories
/// exported in assemblies that exists in the directory specified in
/// the <param name="modulesDirectoryPath" />-parameter.
/// </summary>
/// <param name="mode">The mode the application is running in.</param>
/// <param name="catalog">The catalog used to import ModuleFactory-instances.</param>
public ComposedModule(ApplicationMode mode, string modulesDirectoryPath)
: this(mode, new DirectoryCatalog(modulesDirectoryPath)) { }
#endregion
#region Methods
private void ImportFactories()
{
var batch = new CompositionBatch();
batch.AddPart(this);
var container = new CompositionContainer(this.catalog);
container.Compose(batch);
}
protected override void Load(ContainerBuilder builder)
{
base.Load(builder);
foreach (var factory in this.RegisteredModuleFactories)
{
var module = factory(this.mode);
builder.RegisterModule(module);
}
}
#endregion
}
Usage
public class MyModule : Module
{
protected override void Load(ContainerBuilder builder)
{
base.Load(builder);
builder.RegisterType<SampleJob>().AsSelf();
}
[Export(typeof(ModuleFactory))]
public static ModuleFactory Factory = x => new MyModule();
}
See the post for complete details.
Also, you might want to check out these links:
http://buksbaum.us/2009/12/06/bootstrapping-an-application-with-mef-and-autofac/
http://kalcik.net/2014/02/09/cooperation-between-the-autofac-and-the-microsoft-extensibility-framework/

Best practices for cache and session manager for an MVC Application

Masters,
We have implemented CacheManager and SessionManager in past applications by various ways, like by creating a SessionHelper static class and a CacheHelper static class.
Though it works fine, we lack some ability for generalization and globalization perspective.
So for new scratch development, we intend best practices for such general implementation in terms of flexibility and extensibility.
Please suggest.
You could create an interface to define the common operations used in Caching and Session management, named something like IStateManager. e.g.
/// <summary>
/// An interface to provide access to a state storage implementation
/// </summary>
public interface IStateManager
{
/// <summary>
/// Gets or sets the value associated with the specified key.
/// </summary>
/// <typeparam name="T">Type</typeparam>
/// <param name="key">The key of the value to get.</param>
/// <returns>The value associated with the specified key.</returns>
T Get<T>(string key);
/// <summary>
/// Adds the specified key and object to the state manager.
/// </summary>
/// <param name="key">key</param>
/// <param name="data">Data</param>
void Set(string key, object data);
/// <summary>
/// Adds the specified key and object to the state manager.
/// </summary>
/// <param name="key">key</param>
/// <param name="data">Data</param>
/// <param name="cacheTime">Cache time</param>
void Set(string key, object data, int cacheTime);
/// <summary>
/// Gets a value indicating whether the value associated with the specified key is in the state manager.
/// </summary>
/// <param name="key">key</param>
/// <returns>Result</returns>
bool IsSet(string key);
/// <summary>
/// Removes the value with the specified key from the state manager.
/// </summary>
/// <param name="key">/key</param>
void Remove(string key);
/// <summary>
/// Removes items by pattern
/// </summary>
/// <param name="pattern">pattern</param>
void RemoveByPattern(string pattern);
/// <summary>
/// Clear all state manager data
/// </summary>
void Clear();
}
Then, you could create implementations of the interface to provide different functionality. E.g. an in memory implementation, that uses System.Runtime.Caching
/// <summary>
/// Represents an in memory cache
/// </summary>
public class MemoryCacheManager : IStateManager
{
public MemoryCacheManager()
{
}
protected ObjectCache Cache
{
get
{
return MemoryCache.Default;
}
}
/// <summary>
/// Gets or sets the value associated with the specified key.
/// </summary>
/// <typeparam name="T">Type</typeparam>
/// <param name="key">The key of the value to get.</param>
/// <returns>The value associated with the specified key.</returns>
public T Get<T>(string key)
{
return (T)Cache[key];
}
/// <summary>
/// Adds the specified key and object to the cache with a default cache time of 30 minutes.
/// </summary>
/// <param name="key">key</param>
/// <param name="data">Data</param>
public void Set(string key, object data)
{
Set(key, data, 30);
}
/// <summary>
/// Adds the specified key and object to the cache.
/// </summary>
/// <param name="key">key</param>
/// <param name="data">Data</param>
/// <param name="cacheTime">Cache time</param>
public void Set(string key, object data, int cacheTime)
{
if (data == null)
return;
var policy = new CacheItemPolicy();
policy.AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(cacheTime);
Cache.Add(new CacheItem(key, data), policy);
}
/// <summary>
/// Gets a value indicating whether the value associated with the specified key is cached
/// </summary>
/// <param name="key">key</param>
/// <returns>Result</returns>
public bool IsSet(string key)
{
return (Cache.Contains(key));
}
/// <summary>
/// Removes the value with the specified key from the cache
/// </summary>
/// <param name="key">/key</param>
public void Remove(string key)
{
Cache.Remove(key);
}
/// <summary>
/// Removes items by pattern
/// </summary>
/// <param name="pattern">pattern</param>
public void RemoveByPattern(string pattern)
{
var regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
var keysToRemove = new List<String>();
foreach (var item in Cache)
if (regex.IsMatch(item.Key))
keysToRemove.Add(item.Key);
foreach (string key in keysToRemove)
{
Remove(key);
}
}
/// <summary>
/// Clear all cache data
/// </summary>
public void Clear()
{
foreach (var item in Cache)
Remove(item.Key);
}
}
You could create multiple implementations of this interface, such as a 'Memcached' implementation to provide distributed caching for your application or a 'Session' implementation to provide user session based functionality.
Then, you can use your dependency container of choice to inject the implementations into your services\controllers and wire up your application.
Try and avoid static classes which can be problematic to unit test.
Can use the filter attributes for caching and can handle session through the singleton class..
http://weblogs.asp.net/scottgu/archive/2007/11/13/asp-net-mvc-framework-part-1.aspx
you can get some samples in the above link for the best way or approach to be followed.

Dynatree with ASP.NET MVC

Has anybody got any examples of using the Dynatree plugin with MVC? I have been wrestling with it without much progress. I have an action method which returns a JsonResult (but selects all columns in the underlying table, not sure if this is the problem) and in my initajax call , all I'm doing is calling this method.
If it's not too much trouble, I am looking for sample View and Controller action methods.
Thanks in advance for any help
You need to create an object to serialize the nodes eg.
public interface ITreeItem
{
}
/// <summary>
/// Tree Item Leaf.
/// </summary>
public class TreeItemLeaf :ITreeItem
{
/// <summary>
/// Gets the Title.
/// </summary>
public string title;
/// <summary>
/// Gets the Tooltip.
/// </summary>
public string tooltip;
/// <summary>
/// Gets the key.
/// </summary>
public string key;
/// <summary>
/// Gets the Data.
/// </summary>
public string addClass;
/// <summary>
/// Gets the Children.
/// </summary>
public IList<ITreeItem> children;
/// <summary>
/// Gets the rel attr.
/// </summary>
public string rel;
/// <summary>
/// Gets the State.
/// </summary>
public bool isFolder;
/// <summary>
/// Gets the State.
/// </summary>
public bool isLazy;
/// <summary>
/// Initializes a new instance of the <see cref="TreeItemLeaf"/> class.
/// </summary>
public TreeItemLeaf()
{
children = new List<ITreeItem>();
}
/// <summary>
/// Initializes a new instance of the <see cref="TreeItemLeaf"/> class.
/// </summary>
/// <param name="type">The type of node.</param>
/// <param name="id">The Id of the node.</param>
/// <param name="title">The Title of the node.</param>
/// <param name="tooltip">The Tooltip of the node.</param>
public TreeItemLeaf(String type, Guid id, String title, String tooltip)
{
key = id.ToString();
this.title = title;
isFolder = false;
isLazy = false;
this.tooltip = tooltip;
children = new List<ITreeItem>();
}
}
/// <summary>
/// Tree Item.
/// </summary>
public class TreeItem : TreeItemLeaf
{
/// <summary>
/// Gets the State.
/// </summary>
public new bool isFolder;
/// <summary>
/// Initializes a new instance of the <see cref="TreeItem"/> class.
/// </summary>
public TreeItem() : base()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="TreeItem"/> class.
/// </summary>
/// <param name="type">The type of node.</param>
/// <param name="id">The Id of the node.</param>
/// <param name="title">The Title of the node.</param>
/// <param name="tooltip">The tooltip of the node.</param>
public TreeItem(String type, Guid id, String title, String tooltip) : base(type, id, title, tooltip)
{
isFolder = true;
isLazy = true;
}
}
Once you have this, you can return a Json(IList<ITreeItem>) which you will need to build up from your results..
If you go to the Dynatee demo http://wwwendt.de/tech/dynatree/doc/samples.html , you can use Firefox/Firebug to study the HTTP requests to see exactly what is being passed in and returned.
My tree in the view is as follows :
// --- Initialize first Dynatree -------------------------------------------
$("#tree").dynatree({
fx: { height: "toggle", duration: 500 },
selectMode: 1,
clickFolderMode: 1,
children : #Html.Raw(String.Format("{0}", ViewData["tree"]).Replace("\"children\":[],", "")),
onLazyRead: function (node) {
node.appendAjax({
url: "#Url.Action("treedata", "tree")",
type: "GET",
data: { "id": node.data.key, // Optional url arguments
"mode": "all"
},
error: function(node, XMLHttpRequest, textStatus, errorThrown) {
}
}
});
}, //.... cut short for brevity
I am embeding the initial tree state in the "children:" part. And the Ajax reading is being set up in the "onLazyRead:" part.
My Ajax call is:
public JsonResult TreeData(FormCollection form)
{
return GetTreeData(Request.QueryString["id"], Request.QueryString["uitype"]);
}
The GetTreeData() function returns Json(ITreeItem);
I would recommend you use Firefox/Firebug and its "NET" function to see what is going and coming back.
Hope that helps.
I've just found Dynatree and I'm using it on my MVC project. Here's an example of how I did it. I decided to just put the data directly in the View like the basic example.
My data is a list of cities within California, grouped by county.
My controller simply passes a view model to my View and the view model has a CitiesAvailable property:
public IEnumerable<City> CitiesAvailable { get; set; }
My list of City objects is grabbed from the database (EF4) and the actual City object is the following:
In my View I create a ul containing the list of counties and their cities (I'm using Razor but webforms should be easy enough to figure out):
<div id="tree">
<ul id="treedata" style="display: none;">
#foreach (var county in Model.CitiesAvailable.Select(c => c.County).Distinct().OrderBy(c => c))
{
<li data="icon: 'false'">#county
<ul>
#foreach (var city in Model.CitiesAvailable.Where(c => c.County == county).OrderBy(c => c.Name))
{
<li data="icon: 'false'" id="#city.Id">#city.Name</li>
}
</ul>
</li>
}
</ul>
</div>
Then in my JavaScript I use the following:
$("#tree").dynatree({
checkbox: true,
selectMode: 3,
fx: { height: "toggle", duration: 200 }
});
It works great! Here's a sample of the output with a few items checked:
Let me know if anything doesn't make sense.
Note, I use data="icon: 'false'" in my li elements because I don't want the icons.
You can simply convert the object to json string, and send it to server as text
this is the js code:
var dict = $("#tree").dynatree("getTree").toDict();
var postData = JSON.stringify(dict["children"]);
$.ajax({ type: "POST",
url: "/UpdateServer/SaveUserTree",
data: {tree:postData},
dataType: "html"
});
And this is the controller code:
[HttpPost]
public void SaveUserTree(string tree = "")
{
HttpContext.Application["UserTree"] = tree;
}
You can send this string data back to client
if (HttpContext.Application["UserTree"] != null)
ViewBag.TreeData = new HtmlString(HttpContext.Application["UserTree"].ToString());
And finally, you can initial the tree, in the View with this data:
var treeData= #(ViewBag.TreeData)
$(function(){
// --- Initialize sample trees
$("#tree").dynatree({
children: treeData
});
});

Programatically get controller address of ASP.NET MVC application

Say I want to send an email from my MVC application containing a link back to an item.
So I want something in my controller like:
string link = "www.mysite/mycontroller/itemdetails/23";
can I retrieve this programatically so it works on whatever server/configuration i happen stick it on?
eg
string link = GetCurrentActionRoute() + "23";
public ActionResult Index()
{
string link = Url.Action(
(string)RouteData.Values["action"],
(string)RouteData.Values["controller"],
new { id = "23" },
Request.Url.Scheme
);
// TODO: do something with the generated link
return View();
}
I use the extension methods below from my Controller classes to generate links to various actions.
Examples:
string link = this.Action("Play");
string fullLink = this.FullUrlAction("Play", new { id = 1 });
Methods:
/// <summary>
/// Helper Extensions to the Controller class.
/// </summary>
public static class ControllerExtensions
{
/// <summary>
/// Get an action link for a controller.
/// </summary>
/// <param name="controller">Controller instance.</param>
/// <param name="action">Action to provide the link for.</param>
/// <returns>Link Url for the Action.</returns>
public static string Action(this Controller controller, string action)
{
RouteValueDictionary rvd = new RouteValueDictionary
{
{ "controller", controller.GetType().Name },
{ "action", action }
};
return RouteTable.Routes.GetVirtualPath(controller.ControllerContext.RequestContext, rvd).VirtualPath;
}
/// <summary>
/// Get an action link for a controller.
/// </summary>
/// <param name="controller">Controller instance.</param>
/// <param name="action">Action Name.</param>
/// <param name="parameters">Action Parameters.</param>
/// <returns>Link Url for the action with parameters.</returns>
public static string Action(this Controller controller, string action, object parameters)
{
string controllerName = controller.GetType().Name;
if (controllerName.EndsWith("Controller"))
{
controllerName = controllerName.Substring(0, controllerName.Length - 10);
}
return controller.Action(controllerName, action, parameters);
}
/// <summary>
/// Get an action link for a controller.
/// </summary>
/// <param name="controller">Controller instance.</param>
/// <param name="targetController">Target controller for action.</param>
/// <param name="action">Action Name.</param>
/// <param name="parameters">Action Parameters.</param>
/// <returns>Link Url for the action on the given controller.</returns>
public static string Action(this Controller controller, string targetController, string action, object parameters)
{
RouteValueDictionary rvd = new RouteValueDictionary(parameters)
{
{ "controller", targetController },
{ "action", action }
};
return RouteTable.Routes.GetVirtualPath(controller.ControllerContext.RequestContext, rvd).VirtualPath;
}
/// <summary>
/// Get a fully qualified action link for a controller.
/// </summary>
/// <param name="controller">Controller instance.</param>
/// <param name="action">Action Name.</param>
/// <param name="parameters">Action Parameters.</param>
/// <param name="requestUrl">Current request URI.</param>
/// <returns>Fully qualified Link Url.</returns>
public static string FullUrlAction(this Controller controller, string action, object parameters, Uri requestUrl)
{
string controllerName = controller.GetType().Name;
if (controllerName.EndsWith("Controller"))
{
controllerName = controllerName.Substring(0, controllerName.Length - 10);
}
return controller.FullUrlAction(controllerName, action, parameters, requestUrl);
}
/// <summary>
/// Get a fully qualified action link for a controller.
/// </summary>
/// <param name="controller">Controller instance.</param>
/// <param name="targetController">Target controller for action.</param>
/// <param name="action">Action Name.</param>
/// <param name="parameters">Action Parameters.</param>
/// <param name="requestUrl">Current request URI.</param>
/// <returns>Fully Qualified Link Url.</returns>
public static string FullUrlAction(this Controller controller, string targetController, string action, object parameters, Uri requestUrl)
{
string testUrl = VirtualPathUtility.ToAbsolute(controller.Action(targetController, action, parameters));
return new Uri(requestUrl, testUrl).ToString();
}
}
If the link will point to the same location as that of the application ,you may consider using the Url.Action(...) and its overloads.
Eg.
string actionRoute = Url.Action("action method name","controller name");
If you want to get the complete path, you can use :
string domainRoute = HttpContext.Request.Url.AbsolutePath;
and then concatenate the id with that.

ASP.NET MVC: creating controls dynamically

This is the control builder class...
public class ControlBuilder
{
/// <summary>
/// Html Control class for controlbuilder Control .
/// </summary>
protected HTMLControl formControl;
/// <summary>
/// Html Control class for the label.
/// </summary>
private HTMLControl labelControl;
/// <summary>
/// Getting the property for the Control .
/// </summary>
/// <history>
/// [LuckyR] 10/8/2009 Created
/// </history>
public HTMLControl Form
{
get { return formControl; }
}
/// <summary>
/// Creating a label for the Control.
/// </summary>
/// <history>
/// [LuckyR] 10/8/2009 Created
/// </history>
public HTMLControl Label
{
get { return labelControl; }
}
/// <summary>
/// Creating a construtor for the controlbuilder taking in Zero
/// arguments it creates a labl for the Control .
/// </summary>
/// <history>
/// [LuckyR] 13/8/2009 Created
/// </history>
public ControlBuilder() { }
/// <summary>
/// A construtor for the controlbuilder which
/// creates a label for the Control .
/// </summary>
/// <history>
/// [LuckyR] 10/8/2009 Created
/// </history>
public ControlBuilder(string labelName)
{
Label label = new Label();
label.Text = labelName;
label.Width= 200;
labelControl = new HTMLControl(label);
}
/// <summary>
/// Control build property that is used to biuld the Html
/// markup for the created Control.
/// </summary>
/// <history>
/// [LuckyR] 10/8/2009 Created
/// </history>
public string BuildControl()
{
this.CreateControl();
this.SetAttribute();
return this.RenderHTML();
}
/// <summary>
/// Render Html tags for the Control with label .
/// </summary>
/// <history>
/// [LuckyR] 10/8/2009 Created
/// </history>
public string RenderHTML()
{
return labelControl.RenderHTML() + ": " + formControl.RenderHTML();
}
/// <summary>
/// Used to Set Attributes for the Control .
/// </summary>
/// <history>
/// [LuckyR] 13/8/2009 Created
/// </history>
protected virtual void SetAttribute() { }
/// <summary>
/// Used to create the Control .
/// </summary>
/// <history>
/// [LuckyR] 13/8/2009 Created
/// </history>
protected virtual void CreateControl() { }
/// <summary>
/// A list of all the Controls that will be created during the
/// program run .
/// </summary>
private IList<ControlBuilder> Controls = new List<ControlBuilder>();
/// <summary>
/// A property to add Control to the ControlBuilder that are created by
/// the user.
/// </summary>
/// <history>
/// [LuckyR] 13/8/2009 Created
/// </history>
/// <param name="Control">Controls from the controlbuilder class</param>
public void AddControl(ControlBuilder Control)
{
Controls.Add(Control);
}
/// <summary>
/// A property to display the Controls that are created by
/// the user.
/// </summary>
/// <history>
/// [LuckyR] 13/8/2009 Created
/// </history>
public string Display()
{
string Html = string.Empty;
foreach (ControlBuilder builder in Controls)
{
Html += builder.BuildControl();
Html += "<br /><br />";
}
return Html;
}
}
}
this is how i build a control
public class TextBoxBuilder : ControlBuilder
{
/// <summary>
/// Creating a web Control textBox.
/// </summary>
private TextBox textBox;
/// <summary>
/// Creating an Id to add as an attribute .
/// </summary>
private string Id;
/// <summary>
/// Creating an Value to add as an attribute .
/// </summary>
private string Value;
/// <summary>
/// Creating a Textbox constructor which takes in LabelName and Id.
/// to create a label for the Control.
/// </summary>
/// <history>
/// [LuckyR] 10/8/2009 Created
/// </history>
public TextBoxBuilder(string labelName, string id , string value): base(labelName)
{
this.Id = id;
this.textBox = new TextBox();
this.Value = value;
}
/// <summary>
/// Used to Set properties for the Control .
/// </summary>
/// <history>
/// [LuckyR] 10/8/2009 Created
/// </history>
protected override void SetAttribute()
{
this.textBox.ID = this.Id;
this.textBox.Text = this.Value;
}
/// <summary>
/// Used to create the Control . That is done by calling the HtmlControl class
/// which inturn renders the particular Control for us .
/// </summary>
/// <history>
/// [LuckyR] 10/8/2009 Created
/// </history>
protected override void CreateControl()
{
this.formControl = new HTMLControl(this.textBox);
}
}
}
In my home controller i did this ...
public ActionResult Create()
{
///Where i am contacting the linq to sql classs for performing ths operagtion
foreach (var control in Rep.GetData(ScreenName))
{
string Type = control.Type;
string value = null;
if (id != Guid.Empty)
{
value = DataObj.GetValue(control.TableName, control.ControlName, id);
}
switch (Type)
{
case ("TextBox"):
/// Buliding a textBox box
controlbuilder.AddControl(new TextBoxBuilder(control.Field, control.ControlName, value));
break;
case ("CheckBox"):
/// Bulidig a CheckBox .
controlbuilder.AddControl(new CheckBoxBuilder(control.Field, control.ControlName , value));
break;
case ("DatePicker"):
/// Bulidig a DatePicker .
controlbuilder.AddControl(new DatePicker(control.Field, control.ControlName, value));
break;
case ("DropDownList"):
///Building a dropdownlist.
List<string> list = DataObj.GetDropDownValues(control.Id);
controlbuilder.AddControl(new DropDownListBuilder(control.Field, control.ControlName, list,value));
break;
case ("TextArea"):
/// Building a textBox area .
controlbuilder.AddControl(new TextArea(control.Field, control.ControlName , value));
break;
default:
break;
}
}
return View(controlbuilder);
}
The view page looks like this ...
<% using (Html.BeginForm())
{%>
<fieldset>
<legend>Fields</legend>
<p>
<%= ViewData.Model.Display() %>
</p>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
<div>
<%=Html.ActionLink("Back to List", "Index")%>
</div>
since i am passing my class into the view i can retrieve all the data there with .display .
There is no concept of controls in ASP.NET MVC any longer.
You have two options:
When the user clicks a button, you handle this POST request in a controller action, set some sort of flag in your view model to now show a textbox, then return the same view which in its turn will look at the flag and generate a text box if required. This will incur a complete round-trip which will be somewhat similar to the postback in WebForms.
You do it in-place with JavaScript. You intercept a click event on a button and you inject an input/textarea HTML element into your document structure.

Resources