标签: model-binding

将ASP.NET MVC模型绑定引入ASP.NET WebForm

在ASP.NET MVC中,在[HttpPost]方法上,MVC运行时将根据字段名称自动映射前端表单字段中的数据并将其传输到视图模型中.

如何在ASP.NET WebForm中完成相同的操作?

例如,我有一个名为Person的对象,具有FirstName和LastName属性.

我有一个WebForm页面,文本框控件分别带有FirstName和LastName.

当在表单上按提交时,有没有办法在代码隐藏的Button_Click事件中自动将FirstName和LastName绑定到Person对象?

asp.net-mvc webforms dry single-responsibility-principle model-binding

1
推荐指数
1
解决办法
1427
查看次数

在MVC5中以局部视图提交表单时模型为空

我只是想学习 MVC 并面临一些问题。当我提交我的部分视图时,我在 Create Method 内的Model Blog 中得到 null 。

我做错了什么,正确的方法是什么?

查看(索引.cshtml)

@model IEnumerable<Samples.Controllers.Blog>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>Sample</th>
        <th>URL</th>
        <th>Name</th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
        </td>
        <td>
            @item.URL
        </td>
        <td>
            @item.Name
        </td>
    </tr>
}

</table>

@Html.Partial("_CreateBlog", new Samples.Controllers.Blog()) …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc model-binding razor asp.net-mvc-4 asp.net-mvc-5

1
推荐指数
1
解决办法
841
查看次数

在添加 ApiController 属性之前,ASP.NET Core 3.1 无法处理 Axios 请求

我有以下问题。每当我向 Api 端点发送内容时,ASP.NET Core 3.1 就无法处理该请求。但是,当我添加该ApiController属性时,它工作得很好。

我的代码是正确的,但只有当我添加此属性时才有效。怎么会这样呢?

作为参考,这是我的代码

应用程序编程接口

[ApiController] //Remove this and the code breaks
[Route("api/SomeApi")]
public class ApiController : Controller {

    private readonly IService service;

    public ApiController(IService service)
    {
        this.service = service;
    }

    [HttpPost]
    [Route("Add")]
    public SomeClass Add(SomeClass foo)
    {
        var userId = service.GetCurrentUserId(User);
        foo.Id = Guid.NewGuid();
        foo.UserId = userId;
        service.Add(foo);
        return foo;
    }
}
Run Code Online (Sandbox Code Playgroud)

JS

axios.post('/api/SomeApi/Add', {
   foo: this.foo,

}).then(function (response: any) {
   this.Id = response.Id;
});
Run Code Online (Sandbox Code Playgroud)

仅供参考,我的 ApiController 上还有其他使用 GET/POST 的方法。GET 方法工作得很好,但 POST 方法仅在我使用查询参数时才有效。在本例中,我没有使用查询参数,因为要发送到 …

javascript ajax model-binding asp.net-core axios

1
推荐指数
1
解决办法
6129
查看次数

MVC3视图模型 - 包含列表的复杂对象列表

如何将这种复杂模型与包含多个对象的多个图层绑定?现在我将模型传递给视图 - (填充表单/复选框树),我想要准确的模型(SubjectSelectionModel),但它没有正确绑定.

任何人都可以详细说明我需要采取的过程,以便在我看来正确绑定这些过程吗?

查看型号:

public class SubjectSelectionModel
{
    public IList<Subject> Subjects { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

学科分类:

public class Subject
{
    public String Name { get; set; }
    public IList<Bin> Bins { get; set; }

    public Subject()
    {

    }

    public Subject(IList<Course> courses)
    {

    }
}
Run Code Online (Sandbox Code Playgroud)

Bin类:

public class Bin 
{
    public Subject Subject { get; set; }
    public int Amount { get; set; }

    public IList<Foo> Foos { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

Foo类:

public class Foo
{
    public int …
Run Code Online (Sandbox Code Playgroud)

c# model model-binding asp.net-mvc-3

0
推荐指数
1
解决办法
2578
查看次数

ASP.NET MVC 3模型绑定对我不起作用?

这是不起作用的行:

ModelBinders.Binders.Add(typeof(decimal), new DecimalModelBinder())
Run Code Online (Sandbox Code Playgroud)

更确切地说.粘合剂很高.

我找到了Phil Haack的这篇文章.

我做了所有这一切,添加了对新文件夹using System.Web.Http.ModelBinding.Binders; 添加类的参考,public class DecimalModelBinder : IModelBinder并告诉Global.ascx有关该文件夹.但红线一直显示在Binders下方.你能帮助我吗?

c# model-binding asp.net-mvc-3

0
推荐指数
1
解决办法
1006
查看次数

将登录用户的信用卡号码放入隐藏字段

用户必须登录才能查看其信用卡信息.我想知道在那个时候把这个数字放在一个隐藏的领域是否安全.我必须这样做的原因是因为MVC的模型绑定.如果该字段不存在,那么我将丢失信用卡号码,并且必须进行其他网络服务呼叫.

提前致谢.

security asp.net-mvc model-binding

0
推荐指数
1
解决办法
300
查看次数

wpf MvvM Command数据上下文问题

我用谷歌搜索,但找不到我想要的东西.我有几个问题......

这是我的代码结构:

我的命令类:

 public class BrowseCommand : ICommand 
 {
   //Code here
 }
Run Code Online (Sandbox Code Playgroud)

在ViewModel中:

public class ExampleViewModel
{
    public ExampleViewModel()
    {
        BrowseCommand = new BrowseCommand();
    }

    public ICommand BrowseCommand
    {
        get;
        private set;
    }
  //More Code...
}
Run Code Online (Sandbox Code Playgroud)

MainWindow.xaml.cs:

 public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new ExampleViewModel();
    }
}
Run Code Online (Sandbox Code Playgroud)

MainWindow.xaml:

Window x:Class="Ship32RcParser.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" >

   <Grid>
       <Button  Content="Browse" Command="{Binding BrowseCommand}"/>
       <Button  Content="Search" Command="{Binding I_NEED_HELP_HERE}"  />
   </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)



我知道我的浏览工作正常,因为MainWindow.xaml.cs有

DataContext = new ExampleViewModel(); …
Run Code Online (Sandbox Code Playgroud)

c# wpf mvvm model-binding command-pattern

0
推荐指数
1
解决办法
1万
查看次数

如何在.NET的MVC中对视图模型执行Enum绑定?

如何将枚举绑定到MVC中的下拉列表以使模型在发布后有效?不确定是否需要转换器或其他东西,我提供代码,您推荐的解决方案是什么? (以下代码导致ModelError)

枚举:

public enum TimePlan
{   Routine = 0,
    Single = 1 }
Run Code Online (Sandbox Code Playgroud)

该模型 :

 public TimePlan TheTimePlan { get; set; }

 public SelectListItem[] TimeList { get; set; }
Run Code Online (Sandbox Code Playgroud)

控制器:

    [HttpPost]
    public virtual ActionResult Education(EducationViewModel EducationModelInfo)
    {
        if (ModelState.IsValid)
        { ...
    } }
Run Code Online (Sandbox Code Playgroud)

视图绑定:

@Html.DropDownListFor(m => m.CourseTimePlan, Model.TimeList, "Please select the time plan") 
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc enums type-conversion model-binding

0
推荐指数
1
解决办法
5744
查看次数

asp-for 输入未绑定到 ViewModel

我有一个使用该asp-for属性的简单表单。

@using System.Threading.Tasks
@using MyApp.Controllers
@using MyApp.Models.Login
@model MyApp.Models.Login.LoginModel

<form asp-controller="Login" asp-action="Login" asp-route-returnUrl="@ViewBag.ReturnUrl" method="post" class="form-horizontal" role="form">
  <div class="form-group">
    <label class="col-md-2 control-label">UserName</label>
    <div class="col-md-6">
      <input asp-for="Username" class="form-control"/>
      <span asp-validation-for="Username" class="text-danger"></span>
    </div>
  </div>
  <div class="form-group">
    <label class="col-md-2 control-label">Password</label>
    <div class="col-md-6">
      <input asp-for="Password" type="password" class="form-control"/>
      <span asp-validation-for="Password" class="text-danger"></span>
    </div>
  </div>
  <div class="form-group">
    <div class="col-md-offset-2 col-md-1">
      <button type="submit" class="btn btn-primary">Log in</button>
    </div>
    <div class="col-md-offset-2 col-md-3">
      <a href="/ForgotPassword" class="pull-right">Forgot password?</a>
    </div>
  </div>
</form>
Run Code Online (Sandbox Code Playgroud)

我的控制器有一个接受 LoginModel 的操作,但是当我单击提交时属性为空。

[AllowAnonymous]
[HttpPost("/Login")]
public async Task<IActionResult> Login(LoginModel lm, …
Run Code Online (Sandbox Code Playgroud)

c# asp.net model-binding asp.net-core

0
推荐指数
1
解决办法
1803
查看次数

Asp.Net Core [FromQuery] 绑定

我在使用 [FromQuery] 属性进行模型绑定时遇到问题。

我有以下课程:

public class PaginationSettings
{
    public const int DefaultRecordsPerPage = 5;

    public PaginationSettings(int pageIndex, int recordsPerPage)
    {
        RecordsPerPage = recordsPerPage == 0 ? DefaultRecordsPerPage : recordsPerPage;
        PageIndex = pageIndex == 0 ? 1 : pageIndex;
    }

    public int RecordsPerPage { get; set; }
    public int PageIndex { get; set; }
    public int RecordsStartIndex => RecordsPerPage * (PageIndex - 1);

    public static PaginationSettings Normalize(PaginationSettings source)
    {
        if (source == null)
        {
            return new PaginationSettings(0, 0);
        }

        return new …
Run Code Online (Sandbox Code Playgroud)

.net c# model-binding asp.net-core asp.net-core-webapi

0
推荐指数
1
解决办法
4935
查看次数