Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 581093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:32:33+00:00 2026-05-13T14:32:33+00:00

If I have the below PartialView <%@ Control Language=C# Inherits=System.Web.Mvc.ViewUserControl<Models.Photo> %> <% using (Html.BeginForm(MyAction,

  • 0

If I have the below PartialView

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Models.Photo>" %>

<% using (Html.BeginForm("MyAction", "MyController", FormMethod.Post, new { enctype = "multipart/form-data" }))   { %>

    <%= Html.EditorFor( c => c.Caption ) %>

    <div class="editField">
        <label for="file" class="label">Select photo:</label>
        <input type="file" id="file" name="file" class="field" style="width:300px;"/>
    </div>

  <input type="submit" value="Add photo"/>

<%} %>

As you can see, the Action and the Controller are hard coded. Is there a way I can make them dynamic?

My goal is to have this partial view generic enough that I can use it in many places and have it submit to the Action and Controller it is sitting within.

I know I can use ViewData but really don’t want to and likewise with passing a VormViewModel to the view and using the model properties.

Is there a nicer way than the two I listed above?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T14:32:33+00:00Added an answer on May 13, 2026 at 2:32 pm

    I Checked the source code of MVC and dig into the System.Web.Mvc –> Mvc –> Html –> FormExtensions so I find you can write some code like :

    public static class FormHelpers
    {
        public static MvcForm BeginFormImage(this HtmlHelper htmlHelper,  IDictionary<string, object> htmlAttributes)
        {
            string formAction = htmlHelper.ViewContext.HttpContext.Request.RawUrl;
            return FormHelper(htmlHelper, formAction, FormMethod.Post, htmlAttributes);
        }
    
        public static MvcForm FormHelper(this HtmlHelper htmlHelper, string formAction, FormMethod method, IDictionary<string, object> htmlAttributes)
        {
            TagBuilder tagBuilder = new TagBuilder("form");
            tagBuilder.MergeAttributes(htmlAttributes);
            // action is implicitly generated, so htmlAttributes take precedence.
            tagBuilder.MergeAttribute("action", formAction);
            tagBuilder.MergeAttribute("enctype", "multipart/form-data");
            // method is an explicit parameter, so it takes precedence over the htmlAttributes.
            tagBuilder.MergeAttribute("method", HtmlHelper.GetFormMethodString(method), true);
            htmlHelper.ViewContext.Writer.Write(tagBuilder.ToString(TagRenderMode.StartTag));
            MvcForm theForm = new MvcForm(htmlHelper.ViewContext);
    
            if (htmlHelper.ViewContext.ClientValidationEnabled)
            {
                htmlHelper.ViewContext.FormContext.FormId = tagBuilder.Attributes["id"];
            }
    
            return theForm;
        }
    }
    

    I’m not sure this is exactly what you really want to get but I’m sure you can get it if you change this lines in way satisfies your needs.
    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET MVC Partial View that contains a Html.TextBox that is configured
I am trying to learn some ASP.NET MVC. Please bear with me but asp.net
I am using Jquery to get googlemap data and load it into a div.
Rewritten: My original post seemed to be misunderstood. I have since reported it as
I'm going to re-ask this question in a slightly different way. Say I have
I have Dropdown and on click of a button, I want to display data
I have a usercontrol that is rendering a list of items. Each row contains
Yesterday I re-factored the below method to return either a full view or a
In RC1 the behavior of the template for creating a view changed. As explained

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.