.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan
Santhakumar Munuswamy
Asad Ali
Fauzul Azmi
Post New Web Links

Model Binding the values in an HTML Table back to Controller / Model

Posted By:      Posted Date: August 25, 2010    Points: 0   Category :ASP.Net
 

Currently, I am working with ASP.NET MVC1 and am still learning about Model Binding and how values from a View are passed back to the Controller / Model. Specifically, I want take an existing Model, create a Table and populate the Rows of the Table, allow the user to edit some fields and pass it back.

In my example, I have a Class called "Ingredient" which has 4 public accessors:  Name, Barcode, Amount, and Unit

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcACE.DomainModel.Entities.Ingredient>>" %>

<% using (Html.BeginForm("SubmitOrder","MyController"))
{ %>

<table>
<tr>
<th>Name</th>
<th>Barcode</th>
<th>Amount</th>
<th>Unit</th>
</tr>
<% if (Model != null)
{
foreach (var item in Model)
{ %>
<tr>
<td><%= Html.Encode(item.Name)%></td>
<td><%= Html.Encode(item.Barcode)%></


View Complete Post


More Related Resource Links

How to access property values in the model that you are binding to? - Help!

  

From within a custom modelBinder (derived from DefaultModelBinder), how do I access the existing values of properties in the model into which the new values are being injected?

Thanks in advance for your help!


Help: populate Html.DropDownList from table data? [Linq to SQL model]

  

I have two tables, Users and Roles.  The roles table is a reference table for the different roles of my site (RoleId and RoleName columns). The Users table contains profile data for each user, including a RoleId column that is FK'd back to the Roles table [RoleId].

Both tables are in a Linq to SQL model.

I have an edit page created which lists all the profile values of a user.  The edit page inherits the User model.  One of the editable values is the RoleId field.  I want to set this up to be a drop-down list that contains items labeled using the RoleName column of the Roles table, and matched with values of RoleId.

How can I achieve this?


Binding WPF Controls to an Entity Data Model

  
In this interview, programming writer, McLean Schofield, demonstrates how to bind WPF controls to an entity data model, using Visual Studio 2010 Beta 1. You can also learn more in the topic: Walkthrough: Binding WPF Controls to an Entity Data Model.

Model View Controller (MVC) for ASP.NET

  
Model View Controller (MVC) architectures, immediately wonder how the MVC pattern can be applied in an ASP.NET world. This article will demonstrate how the MVC pattern can be applied to an ASP.NET web application and still maintain the basic Microsoft supplied architecture.

Binding WPF Controls to an Entity Data Model

  
In this interview, programming writer, McLean Schofield, demonstrates how to bind WPF controls to an entity data model, using Visual Studio 2010 Beta 1. You can also learn more in the topic: Walkthrough: Binding WPF Controls to an Entity Data Model.

Entity Data Model and database view returning the same columns as there are in a table

  

When adding a stored procedure into the Entity Data Model I can select whether the procedure returns a scalar, a (new) complex type or one of the entity types I already defined. 

How do I do something similar for a view?


I mean assuming I have a view like this

CREATE VIEW FilteredFoos as SELECT Foo.* FROM Foo join ... WHERE ...

(that is a view that implements some involved filtering, but returns all columns from one table) how do I add it to the project so that I can use the entity set, but get the Foo objects, not some new FilteredFoo objects.


var foos = myDB.FilteredFoos.Include("Bar").ToList();

foreach (Foo foo in foos) { ...


Thanks, Jenda


Html.RenderPartial appears to be stripping Model of custom attributes

  

Hello,

I have implemented a custom ModelMetadataProvider so that I can decorate my view models with some custom attributes and everything was working fine until I made use of a Partial View.

The following code in my view works fine: -

<%: Html.DisplayFor(x => x.Results) %>

Results is a List which renders a custom display template and is also decorated with a custom attribute.  Using breakpoints, after the above line and prior to the code within the custom display template, the overridden CreateMetadata method in my custom ModelMetadataProvider is invoked.  If I look at the attributes collection parameter I can see that it does contain my custom attribute thus everything working as expected.

However, if I replace the above with the following line of code in my view then it breaks: -

<% Html.RenderPartial("ApplicationSearchResults", Model.Results, new ViewDataDictionary()); %>

All the Partial View contains is: -

<%: Html.DisplayFor(x => x) %>

Again using breakpoints, after the above line and prior to the code within the custom display template, the overridden CreateMetadata method in my custom ModelMetadataProvider is invoked.  But this time if I look at the attributes collection parameter my custom attribute is not there.

Any idea

Html.BeginForm and Returning Model

  
Any idea how can View return model(myModelValues) on the click of submit button,aspx code     <% Html.BeginForm("submit", "RenewalManager", FormMethod.Post); %>    <input type="submit" name="submitButton" onclick=" return confirmReject()" value="Test" />    <% Html.EndForm ( ); %> controller code        public ActionResult submit(string submitButton)        {            switch (submitButton)            {  //Cases...                case "Test":                    // call another action to perform the cancellation                     return (Confirm());  //...             }        }        public ActionResult Test()        {  

Pass values From Html.BeginForm To Controller

  
I'm using 2 submit buttons. <form id="form1" runat="server"> First name :<asp:TextBox ID="FNanme" runat="server" /><br /> Last name :<asp:TextBox ID="LName" runat="server" /><br /> </form> <% Html.BeginForm("ActionName", "ControllerClass", FormMethod.Post); %> <input type="submit" name="submitButton" onclick=" return popUp1Msg()" value="Reject" /> <input type="submit" name="submitButton" onclick=" return popUp2Msg()" value="Accept" /> <% Html.EndForm ( ); %> On every submit, Popup appears with Ok and Cancel buttons.When user select Ok. How can I pass FName and LName values to controller? Thanks  

What is the correct error handling in custom model binding?

  
Hello,I am working on a Custom Model Binder where a certain error can occur.When this happens it returns null and a error is added to the ModelState.The problem is that in some projects I am using Fluent Validation and the errors messages I am adding through FV are not taking effect.The message added by the Model Binder always prevails.Should the Model Binder add error messages or just bind the field and return null if some problem happened?What would be the correct implementation for this?Thanks,Miguel

Many-To-Many table relationship and model.edmx

  
Hello,I'm having a weird problem with the database model as it seems. I have 3 tables (posts, taggedpost, tags) and when I create the model the relationship is correctly identified as a many-to-many but the linking table is gone.Instead, VS2010 creates only 2 tables, and maps 'tags' as a "Navigation Property" of 'content' and vice versa.So problem is, I need to query for posts that have a specific tag. The best I have gone so far is to query for all posts that have at least one tag and fetch them then programatically filter them, but I want the optimal which is to select them all at once.

How to exclude one of model's property from the Html.EditorFor

  
Hi, I would like to exclude one of model's property from the Html.EditorFor. I tried  [HiddenInput(DisplayValue=false)]  but it renders the property as an hidden input. This could potentially be a security problem. How can we completely exclude a field from Html.EditorFor? Thank you,Max

Controller-Model-View

  

Dear All,

I have

- controller (customer)

public class customer{

public string Name { get; set; }

public string sex {get;set;}

}

- model (customermodel)

public class customermodel{

public string Name { get; set; }

public string sex {get;set;}

}

- view (index.aspx)

so i want to transfers presentation model to view (use IList and IEnumberable), how can i do? after i want to display data from database, how can i do ?


Thanks

nara


Movie DB APP Accessing your Model's Data from a Controller

  

Tutorial is going grat I am learning.  I have had to fiqure out a few errors along  the way but the VWD IDE makes debugging doable.  Until NOW I have ran into the problem that when adding a new view the requested class Movies1.Models.Movie is not available in the pull down menu.  If it is entered manually then the List option is not active in the next pulldown.  At this point I have to ask a question that reaveals my experience level which is why I am working on a beginner level tutorial.  Does running the dubbing function accomplish the compile taks.  Does the build function?  I ask because the instructions remind that the classes will not be availible if the program has not been compiled.  Here is what I have

sing System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Movie1.Controllers
{
    public class MoviesController : Controller
    {
        MoviesEntities db = new MoviesEntities(); 
        
        

        public ActionResult Index()
        {
            var movies = from m in db.Movies
                         where m.ReleaseDate > new DateTime (1984, 6,1,)
                         select m ;
            
            return View(movies .ToList());
        }

    }
}


I read the FAQ's and searche

Returning a model and a html fragment

  

I have a web page with a form in the footer, when the user clicks submit in this form I submit their data and then return the view with the model with the line

Return View("TalkToUs", New TalkToUsModel(customer))


but this means that the user is at the top of the page and cannot immediately see the validation errors or success message on that page as this information is posted in the footer section so is there a way to return a view with the model and a html fragment so the url is 

http://website/page#footer so that they can see this straight away without having to scroll down.

Also I have to assume the user has javascript turned off and the user could be using browsers ie6 and greater.


Thanks in advance for any help.


Problem binding a collection to a view model on POST when manipulating the DOM with javascript.

  

Hi, i'm currently working on a strongly typed view and i'm binding a collection to a table like this:

<%

for (int i = 0; i < Model.Cars.Count; i++)

{

%>

<tr>

<td>

<%= Html.Hidden(String.Format("Cars[{0}].Id", i),

                                        Model.Cars[i].Id)%>

</td>

<td>

<%= HtmlTextBox(String.Format("Cars[{0}].CarModel", i),

                                        Model.Cars[i].CarModel,

                         

How to Pass data(not the part of model) from View to Controller?

  

How Can I Pass data(not the part of model) to Controller from View?
View Code

<%= Html.ActionLink(item.Name, "ActionName", "ControllerName", new { companyID = Html.Encode("abc"), ID = Html.Encode("xyz") })%>


Controller Code

public ActionResult ActionName(string companyID , string ID)
{.
}


Any idea?


Categories: 
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend