将模型数据从View传递到Controller并使用其值 [英] Passing Model data from View to Controller and using its values

查看:88
本文介绍了将模型数据从View传递到Controller并使用其值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从视图发送数据,并在控制器中使用它来为我正在处理的上传文件功能构建文件名,下面是我的代码.

I'm trying to send data from the view and use it in the controller to construct a filename for an upload file feature I am working on, my code is below.

控制器

    // GET: File
    [Authorize(Roles = "Admin, Lecturer")]
    public ActionResult Index()
    {



        foreach (string upload in Request.Files)
        {
            if (Request.Files[upload].FileName != "")
            {
                string path = AppDomain.CurrentDomain.BaseDirectory + "/App_Data/uploads/";
                string filename = Path.GetFileName(Request.Files[upload].FileName);
                Request.Files[upload].SaveAs(Path.Combine(path, filename));
            }
        }
        return View();
    }

模型

public class UploadModel
{
    [Required(ErrorMessage = "Course is required")]
    public string Course { get; set; }
    [Required(ErrorMessage = "Title is required")]
    public string Title { get; set; }

    public string Uploader { get; set; }
}

查看

<div class="uploadContainer">
    <table>


        <tr>
            <td>Title :</td>
            <td colspan="2" class="editPostTitle">
                @Html.TextBoxFor(tuple => tuple.Item1.Title, new { @class = "uploadTitleInp" })
                @Html.ValidationMessageFor(tuple => tuple.Item1.Title)
            </td>
        </tr>

        <tr>
            <td>Course :</td>
            <td>
                @{
                    List<SelectListItem> listItems = new List<SelectListItem>();
                    foreach (var cat in courses)
                    {
                        listItems.Add(new SelectListItem
                        {
                            Text = cat.Course.Name,
                            Value = cat.Course.Name
                        });
                    }
                }

                @Html.DropDownListFor(tuple => tuple.Item1.Course, listItems, "-- Select Status --")

                @Html.ValidationMessageFor(tuple => tuple.Item1.Course)
            </td>
        </tr>

        <tr>
            <td>File :</td>
            <td>
                <input type="file" name="FileUpload1" id="fileUpload" required />
            </td>
        </tr>

        <tr>
            <td></td>
            <td>
                <input id="btnUploadFile" type="button" value="Upload File" />
            </td>
        </tr>

    </table>

</div>

此方法负责将上载的文件放置到目录中.我想要做的就是通过执行以下操作来创建文件名.

This method is responsible for placing the file that is uploaded into the directory. What I want to be able to do is create a file name by doing something like this.

string filename = model.Title + " - " + model.Course;

我通常知道在使用db存储数据时如何实现此目的,但是由于我没有存储在db中上传的文件,因此我真的不知道如何将模型数据传递给控制器​​,因此我可以使用用户输入的值来构造文件名.我对这个框架和语言还比较陌生,因此可以极大地帮助您使用任何帮助和指针.

I usually know how to achieve this when using a db to store data but since I am not storing the files that are uploaded in a db then I don't really know how to pass the model data to the controller so I can use the values that have been entered by the user to construct the file name. I am relatively new to this framework and languages so any help and pointers would be greatly appriciated.

谢谢!

推荐答案

您必须将数据通过模型传递回单独的控制器方法.可以按以下方式实现(我已经简化了您的代码,但是一般的实现应该可行):

You have to pass the data back through your model to a separate controller method. This can be implemented as follows (I've simplified your code a bit, but the general implementation should work):

public class UploadViewModel
{    
    public string Course { get; set; }
    public string Title { get; set; }
}

您的GET操作:

public ActionResult Index()
{
    return View(new UploadViewModel());
}

然后在您的视图中添加模型并在表单中使用它,以便将数据绑定到您的模型.然后可以将其发送回您的控制器.

Then in your view add the model and use it in the form, so that the data will be bound to your model. This can then be send back to your controller.

@model UploadViewModel
@using(Html.BeginForm())
{
    Course: @Html.TextBoxFor(s=>s.Course)
    Title: @Html.TextBoxFor(s=>s.Title)
    <input type="submit" value="Save file" />
}

现在通过控制器中的HttpPost操作方法获取值:

Now get the values through a HttpPost action method in your controller:

[HttpPost]
public ActionResult Index(UploadViewModel model)
{
    //You can use model.Course and model.Title values now
}

这篇关于将模型数据从View传递到Controller并使用其值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆