带有html属性的html.BeginForm asp.net mvc4

时间:2012-06-10 21:39:04

标签: c# razor asp.net-mvc-4

我有Html.BeginForm的编辑操作。如何添加HTML属性?

我只知道一种方式:

@using (Html.BeginForm("Edit", "Clients", FormMethod.Post, new { @class="example"})) {

}

但如果我使用此方法,则无法传递当前ID

是否可以在不修改操作URL的情况下向表单添加HTML属性?

4 个答案:

答案 0 :(得分:36)

您需要的覆盖是:

@using( Html.BeginForm("Edit", "Clients", new { Id=Model.Id},
                       FormMethod.Post, new { @class = "example" } ) )
{
}
  • 路由值如“id”将作为第三个参数传递。
  • 像“class”这样的HTML属性作为第五个参数传递。

请参阅MSDN文档。

答案 1 :(得分:10)

Action和Controller参数也可以为null,以使用默认操作:

import UIKit
import CoreData

enum MenuState {
    case Collapsed 
    case Expanding  
    case Expanded 
}

class ContainerViewController: UIViewController {

    weak var managedObjectContext: NSManagedObjectContext!

    weak var mainViewController: MasterViewController!

    weak var menuViewController: MenuViewController?

    var currentState = MenuState.Collapsed {
        didSet {
            let shouldShowShadow = currentState != .Collapsed
            showShadowForMainViewController(shouldShowShadow: shouldShowShadow)
    }
}

override func viewDidLoad() {
    super.viewDidLoad()

    mainViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "mainView") as! MasterViewController
    view.addSubview(mainViewController.view)

    DispatchQueue.global(qos: .userInteractive).async {
        self.mainViewController.managedObjectContext = self.managedObjectContext
        self.addChildViewController(self.mainViewController)
        self.mainViewController.didMove(toParentViewController: self)
    }

    let panGestureRecognizer = UIPanGestureRecognizer(target: self, action:#selector(self.handlePanGesture(_:)))
    self.mainViewController.view.addGestureRecognizer(panGestureRecognizer)

    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action:#selector(self.handleTapGesture))
    self.mainViewController.view.addGestureRecognizer(tapGestureRecognizer)
}


@objc func handlePanGesture(_ recognizer: UIPanGestureRecognizer) {

    switch(recognizer.state) {

    case .began:

        let dragFromLeftToRight = (recognizer.velocity(in: view).x > 0)
        if (currentState == .Collapsed && dragFromLeftToRight) {
            currentState = .Expanding
            addMenuViewController()
        }

    case .changed:

        let positionX = recognizer.view!.frame.origin.x + recognizer.translation(in: view).x
        recognizer.view!.frame.origin.x = positionX < 0 ? 0 : positionX

        let boundX = mainViewController.view.frame.width - menuViewExpandedOffset
        if positionX > 0 {
            recognizer.view!.frame.origin.x = positionX > boundX ? boundX : positionX
        }
        recognizer.setTranslation(.zero, in: view)

    case .ended:

        let boundX = mainViewController.view.frame.width - menuViewExpandedOffset
        let hasMovedhanHalfway = (recognizer.view!.frame.origin.x > boundX * 0.3)
        animateMainView(shouldExpand: hasMovedhanHalfway)

    default:
        break
    }
}

@objc func handleTapGesture() {
    if currentState == .Expanded {

        animateMainView(shouldExpand: false)
    }
}

//ADD MENUVC
func addMenuViewController() {
    if (menuViewController == nil) {
        menuViewController = UIStoryboard(name: "Main", bundle: nil)
            .instantiateViewController(withIdentifier: "menuView")
            as? MenuViewController

        view.insertSubview(menuViewController!.view, at: 0)

        addChildViewController(menuViewController!)
        menuViewController!.didMove(toParentViewController: self)
    }
}

let menuViewExpandedOffset: CGFloat = 480


func animateMainView(shouldExpand: Bool) {

    if (shouldExpand) {

        currentState = .Expanded
        animateMainViewXPosition(targetPosition: mainViewController.view.frame.width -
            menuViewExpandedOffset)
    }

    else {
        animateMainViewXPosition(targetPosition: 0) { finished in

            self.currentState = .Collapsed

            self.menuViewController?.view.removeFromSuperview()
            self.menuViewController?.removeFromParentViewController()
            self.menuViewController = nil
        }
    }
}



func animateMainViewXPosition(targetPosition: CGFloat,
                              completion: ((Bool) -> Void)! = nil) {

    UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 1.0,
                   initialSpringVelocity: 0, options: .curveEaseOut, animations: {
                    self.mainViewController.view.frame.origin.x = targetPosition
    }, completion: completion)
}

func showShadowForMainViewController(shouldShowShadow: Bool) {
    if (shouldShowShadow) {
        mainViewController.cameraBGimage.layer.shadowOpacity = 0.6
        mainViewController.cameraBGimage.layer.shadowOffset = CGSize(width: 0, height: 1)
    } else {
        mainViewController.cameraBGimage.layer.shadowOpacity = 0.0
    }
}


}

答案 2 :(得分:2)

通过ControllerA的ActionLink调用

@using (Html.BeginForm("Create",
    "StudentPChoice",
    new { StudentPChoiceId = Model.StudentPChoiceId },
    FormMethod.Post))
{

}

OR

@using (Html.BeginForm("Create",
    "ControllerB",
    new { ControllerBId = Model.ControllerAId },
    FormMethod.Post))
{

}

答案 3 :(得分:0)

如果这可能对某些人有帮助,这对我有用:

@using (Html.BeginForm("RefreshData", "Home", FormMethod.Post, 
        new { Id = "timerangeId", @name = "timerange" }))
    {
        // form elements and input
    }

在Java语言中:

document.getElementById("timerangeId").submit();