Use Same Form For Add And Edit When Specifying Html Attributes?
Dec 2, 2010
I wish to use the same form for adding and editing records within a database using a partial view. I understand that this is fine as the standard Html.BeginForm automatically output the correct html depending on the action that is being used (Add / Edit). However, I need to out said form with some extra HTML attributes. There does not appear to be an overload that allows this to happen without also specifying the ACTION and CONTROLLER names. If I hardcode these then surely I cant use the same form for edit and add automatically? Or am I missing something?
I have a page where I have a tab container and four tabs. the problem is on the first tab "manage quote request" I have a Gridview with the quote request general informatin listed and have a template field created with a link button to databind to the formview quote details. the porblem comes in when after I click on the select linkbutton. the Formview loads right. Then Whe I try to click on the edit linkbuttin in the form view I have to click it twice to change the mode to edit.
I have a web form with usual elements (first name, last name, etc). The Postback URL is a different website altogether as the form is intented to post lead information to another website.
The site that accepts the lead is expecting First Name to come over as "FName", and Last Name to come over as "LName". Is there any way I can set the ID of a textbox to "txtFName", but submit it over the wire as "FName"? I tried changing the name attribute, but at runtime it sets the name = id.
I need to add some attributes [URL] to the tag in an ASP.NET Page object. Note: I cannot do this in a declarative manner and have to use the server side object model to do it.
To add some additional information:
I need to do this within the ASP.NET Page rendering life cycle.
I need to add the attribute to the root element in the page.
Check out the following: <a href="/test?x=@if (Model.IsTest) { @(1) } else { @(4) }"></a> Is there a better way to write this instead of the @(1) and @(4)?
tried many approaches like below to determine which web page form buttons are checked and then get all the attributes for those checked buttons, getting nowhere fast, note on the sample code below, a button was checked but nothing was found with this code, there's got to be a way to this, yes?
Dim MyControl As Control Dim MyRadioButton As RadioButton For Each MyControl In Page.Controls If MyControl.GetType().FullName.ToString = "System.Web.UI.WebControls.radio" Then MyRadioButton = MyControl If MyRadioButton.Checked = True Then foo = MyRadioButton.Attributes("name") End If End If Next
this is what the radio buttons are like they have unique id's and values, its the name attribute that groups them
I would like to add html attributes to form inputs, specifically disabled="disabled" in addition to others. I have conditional logic in the Controller that determines whether to add this html attribute or not that sets a bool IsDisabled flag in my ViewModel e.g.
Except that Add returns void, and there is no such thing as AddConditional. Is there something like an MVC HtmlAttributeBuilder class, or should I just extend Dictionary with these two extension methods?
I have a form that should have all html elements disabled unless a user id is present, indicating that a user has been loaded for editing. I've done this, which works:
[Code]....
This works, and all is good. I couldn't set an if statement up to control creation of the entire Html.BeginForm, which was a bit of a pain. When I tried, the View thought I was creating 2 form begin tags with only one end tag, even though it was inside a conditional statement. Whatever. The problem I now have is that I need to assign an id for the form to work with a validation plugin. I can't add the id = "someID" inside of the condition, because it won't be available when the user id is present, which completely defeats the purpose. I can't move the ternary condition to inside of the 'new' statement - I tried. I thought this would work:
[Code]....
But no dice. I get the following error: Type of conditional expression cannot be determined because there is no implicit conversion between 'AnonymousType#1' and 'AnonymousType#2'. What? Seriously? I'm not trying to convert anything. I'm either assigning a new html attributes section with one set of values or another set of values. Since I can't perform the conditional logic inside of the 'new' statement, I have to recreate the entire 'new' statement after the condition is tested, but this still doesn't work, as I get the aforementioned error. As a short-term fix, I'm going to just render an actual html form tag with the required data, but I'd like to know why it is that code executed/rendered within conditional statements doesn't understand that both true and false results aren't running, and also how to fix this issue while still using the Html.BeginForm helper.
I have an odd error with an ASP.NET web page (ASP.NET 2.0, C#). For several users at one customer location, on one part of one page, HTML content and attributes are being stripped out. So, something that should look like this:
<p class="adminmainlink"> <a href="ad_resourcewizard.aspx">Add or edit resources</a> <script type="text/javascript">
public class CustCtl : WebControl { protected override System.Web.UI.HtmlTextWriterTag TagKey { get { return HtmlTextWriterTag.Div; } } }
With this bare bones control, it would render the root element as a Div tag. But how can I add attributes to that root HTML element that this control will render .
i am working an asp.net mvc 2 web app using model metadata and some of the model metadata don't seem to work when using the default Html.EditorForModel().For example, when applying the DefaultValue(1) and the ReadOnly(true) attributes on a model field, the field displayed on edit view has zero for its default value and it is not read only.
I have a fck editor in which the user enters some text. And in the code i want to strip the class,id attributes of the text posted. I know this can be done through regular expressions And i have written some code to do so but unfortunately it's not working.
private string RemoveScripts(string input) { string re1 = "(.*?"; // Non-greedy match on filler string re2 = "(class)"; // Word 1 string re3 = "(=)"; // Any Single Character 1 string re4 = "(".*?"))"; // Double Quote String 1 string re5 = "(id)"; Regex regClass = new Regex(re1 + re2 + re3 + re4, RegexOptions.IgnoreCase | RegexOptions.Singleline); Regex regID = new Regex(re1 + re5 + re3 + re4, RegexOptions.IgnoreCase | RegexOptions.Singleline);
input = regClass.Replace(input, new MatchEvaluator(ReplaceClassID)); input = regID.Replace(input, new MatchEvaluator(ReplaceID)); return input; } private string ReplaceClassID(Match m) { return ""; }
I am using DataAnnotations to supply information for Inserting/Editing data into a grid. Is there a way to control the Html attributes of the DisplayNames I use, so that, for instance, the DisplayNames can be a different color from the text in the textboxes?
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.
I'm pretty new to MVC 2 using the Entity Framework. I have two tables Company {ID int identity PK,Name nvarchar} and User {ID int identity PK,UserName nvarchar,CompanyID int FK}. A Foreign Key exists between User and Company.I generated my ADO.NET Entity Data Model, a Controller and a view to insert a record. My HTML form has the fields Company and UserName and the is when I click save a Company and User is inserted into the database. Sounds straight forward right!
My question is as follows:
I created a strongly-typed view derived from my 'User' entity. I'm using the the html helper Html.TextBoxFor(model => model.Organisation.Name) but the html name attribute for this input field is 'Organisation.Name'. My problem with this is that the dot throws up all sorts of issues in JQuery, which sees this as a property. If I want to change the name I read that I can use DataAnnotations but because I used the Entity Designer this involves using Buddy Classes. Seems like a bit of overkill just to change the html name attribute on this input field.
In VS2008, it used to be that whenever I was typing an html attribute in an .aspx page when I hit '=' a pair of double quotes was automatically inserted and the cursor placed inside them. I guess I've changed a setting, but I don't know what to change to get that functionality back. I am using Resharper if it makes a difference.
I have a user control that displays a list of records. Users have the ability to open an embedded user control with a FormView to either add a new record or to edit an existing one. The UserControl with the FormView has a button that closes and hides the user control.
If the user control with the FormView is opened to edit a record, the code works fine. But if the UserControl is opened directly into Insert mode, the code craps out
I am accessing this xml file by using LINQ to XML in asp.net by using C#. I am able to access all the attributes of an XML node by explicitly specifying the name of the attribute. I want to write query on this xml file which reads all the attribute values of the xml node (In our example the node is SERVER) dynamically means I want to write the query which can read the read the value of the attribute Name & ID from first node, only name from second row, Name, ID & Parent from the third row , Name & ID from the fourth row, Name, Parent & Value from the fifth row & only Name from the sixth row without modifying the existing code every time. Once I add one of the attribute ( for example if I add the attribute ID in the sixth row ) in the above xml file then I dont need to modify my LINQ to XML query. My query should dynamically fetch the total number of attributes & display their values. Is their any way to do this ?
I have a simple ASP.NET page that uses the VLC media player to play a video in IE. I also have four buttons to control the playback:
Play, Pause, Stop, and Mute
The four buttons call JavaScript functions that access the ActiveX control. When I click on any of the buttons, I get the following error in the JavaScript function:
"Microsoft JScript runtime error: 'vlc' is undefined".
However, if move the object tag for the vlc player outside the form tag, then the JavaScript works correctly, and I can control the video playback.
My question is why must the object tag be outside the form tag for this code to work correctly?
I have an Edit form that utilizes the following ViewModel:
[Code]....
SUserDetail, User and CUDetail are LINQtoSQL classes in my [ .dbml ]. What the above ViewModel allows me to do, is provide a nice and neat Edit form populated with data from the above 3 objects.
However I am stuck on how to handle the HTTP Post for the Edit Action.